/*내용을 복사 한 후 이클립스에서

ctrl + shift + F를 눌러주세요!!*/

 

//생성자에서 다른 생성자 호출하기 this(), this
//생성자 끼리 서로 호출해서 쓰는거랍니다..굳이 그럴 필요가 있을까??싶다..한번 보기로 하지요~
//바로 예문입니다.

package JavaTest;

class Car2{
 String color;
 String gearType;
 int door;
 
 Car2(){
  this("white","Auto",4);       //헐..이게 머야..이렇게 바로 쓰면 빨간줄이..후덜덜..;; 근데 이놈은 코딩하는놈이 이런
 }                                        //생각을 하고 쓴거야.. "난 밑에다가 Car 생성자를 만들껀데..매개변수가 저렇게 3개 있는
                                          //놈을 쓸거야..그러니 3개 있는놈을 가져다 쓸거라서 this 하고 3개의 매개변수 지정한거야!"라고.
 
 Car2(String color, String gearType, int door){    //요거 앞에서 본거다.ㅋ 매개변수에 저렇게 주소값이 아닌 값을 정확히
  this.color = color;                                    //지정하면 여기서처럼 자기자신 값 color = color 다..라고 하는 불상사??
  this.gearType = gearType;                        //가 생기기 때문에..this.라고 명시를 해주는거였구나..이거 무슨 규칙같다.
  this.door = door;
 }
 
 Car2(String color){                     //넌 또 머니? 매개변수는 딸랑 하나...
  this(color,"stick",2);             //3개짜리 호출했는데..color 만큼은 자기 자신것을 쓴다고 하네..
 }
 
 
 
}

public class CarTest2 {

 public static void main(String[] args) {

  //결과를 한번 출력해보자~ 인스턴화~
  Car2 c1 = new Car2();            //매개변수가 아무것도 없는놈 하지만..3개짜리를 불러서 쓰기때문에..
  
  Car2 c2 = new Car2("black");   //이놈은 매개변수 한개짜리 생성자 인스턴스 시키네..
  
  //두개만 결과를 따지고 보면
  
  System.out.println(c1.color+","+c1.gearType+","+c1.door);
  System.out.println(c2.color+","+c2.gearType+","+c2.door);
  
  //그럼 두번째에 있는 생성자(3개짜리)도 시간되는데 한번 써먹어볼께~다이렉트로!
  
  Car2 c3 = new Car2("yellow","Auto",3);
  System.out.println(c3.color+","+c3.gearType+","+c3.door);  //괜찮네..그려~~ㅋㅋ
  
 }

}

 

Posted by 옆모습오빠
: