1. 예제
-> 생성자를 사용해보자!! - 아래에 멤버변수 선언
2. 코드
package java_learn;
class Tv {
int ch;
int vol;
boolean power;
int inch;
int price;
String color;
void powerOnOff() {} //기능 - tv켜고 끄기
void chUp() {} //기능 - 채널 위로
void chDown() {} //기능 - 채널 아래로
//생성자 만듬 -> 단축키 alt + shift + s -> o
public Tv(int ch, int vol, boolean power, int inch, int price, String color) {
this.ch = ch;
this.vol = vol;
this.power = power;
this.inch = inch;
this.price = price;
this.color = color;
}
}
public class java1 {
public static void main(String[] args) {
//생성자를 만들면 선언할 때 생성자 만들때 선언된 멤버변수 값을 전부 입력해주어야한다.
Tv samsungTv = new Tv(10,5,false,80,6000000,"white");
Tv lgTv = new Tv(5,5,false,40,3000000,"black");
System.out.println(lgTv.color);
}
}
'IT > JAVA' 카테고리의 다른 글
JAVA - 실습 - 다마고치 키우기 (0) | 2023.06.13 |
---|---|
JAVA - 예제 - 간단한 TV 전원 및 볼륨 조절 프로그램(리모컨) (0) | 2023.06.13 |
JAVA - 예제 - this (0) | 2023.06.12 |
JAVA - 예제 - 클래스 (Class) 선언 (0) | 2023.06.12 |
JAVA - mini 프로젝트 - 키오스크 만들기 (0) | 2023.06.11 |
댓글