본문 바로가기
IT/JAVA

JAVA - 예제 - 생성자

by 비준 2023. 6. 12.

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);

}
}

댓글