티스토리 뷰

package day160708;


class Phone{

String model, maker, color;

int count;

Phone(String model){

this(model, "강남점", 25);

}

Phone(String model, String maker, int count){

this(model, maker, count, "white");

}

Phone(String model, String maker, int count, String color){

this.model = model;

this.color = color;

this.count = count;

this.maker = maker;

}

void phonePrint(){

System.out.println("=-=-=-=-=-= Phone =-=-=-=-=-=-");

System.out.println("Model : " + model);

System.out.println("Color : " + color);

System.out.println("Count : " + count);

System.out.println("Maker : " + maker);

System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");

}

}


public class day160708_03 {


public static void main(String[] args) {

Phone p1 = new Phone("Glaxy A5");

Phone p2 = new Phone("Glaxy A7", "구로점", 185);

Phone p3 = new Phone("Glaxy S6", "강남점", 115, "White");

Phone p4 = new Phone("Glaxy Ace", "수원점", 5, "Black");

p1.phonePrint();

p2.phonePrint();

p3.phonePrint();

p4.phonePrint();


}


}




=-=-=-=-=-= Phone =-=-=-=-=-=-

Model : Glaxy A5

Color : white

Count : 25

Maker : 강남점

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

=-=-=-=-=-= Phone =-=-=-=-=-=-

Model : Glaxy A7

Color : white

Count : 185

Maker : 구로점

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

=-=-=-=-=-= Phone =-=-=-=-=-=-

Model : Glaxy S6

Color : White

Count : 115

Maker : 강남점

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

=-=-=-=-=-= Phone =-=-=-=-=-=-

Model : Glaxy Ace

Color : Black

Count : 5

Maker : 수원점

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



약간 심화된 예제입니다.

this 문과 메소드 오버로딩과 생성자를 동시에 사용하는 예제인데요.


먼저 인스턴스를 생성할때 자료형과 갯수가 일치하는 메소드를 실행시키고, this 문을 통해 부가 내용은 자동적으로

추가합니다. 


그리고 마지막에 모델명, 색, 남은갯수, 구입처가 모두 설정되면 변수에 저장, print 문으로 실행하는것이죠.


'Java' 카테고리의 다른 글

자바 this 사용 예제  (0) 2016.07.08
자바 생성자 예제  (0) 2016.07.08
자바 메소드 오버로딩 예제 [2]  (0) 2016.07.07
자바 메소드 오버로딩 예제  (0) 2016.07.07
자바 return 문과 인자값 받기 예제  (0) 2016.07.07
댓글
05-21 03:18
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함