관리 메뉴

nalaolla

오브젝트 예제 2 (this : 초기화 코딩을 복제하는 기능) 본문

JAVA/7. Object3

오브젝트 예제 2 (this : 초기화 코딩을 복제하는 기능)

날아올라↗↗ 2016. 6. 22. 17:16
728x90
  1. package test.com;
  2.  
  3. public class Test02Member {
  4.  
  5.     int num; // 회원번호
  6.     String id;
  7.     String pw;
  8.     String name;
  9.     String tel;
  10.  
  11.     public Test02Member() {
  12.         num = 99;
  13.         id = "aaa";
  14.         pw = "bbb";
  15.         name = "ccc";
  16.         tel = "ddd";
  17.     }
  18.    
  19.     public Test02Member(String id) {
  20.         this();    // 여기 사용된 this는 인자값 없는 생성자 Test02Member()를 바라본다..
  21.         this.id = id;
  22.     }
  23.  
  24.     public Test02Member(String id, String pw) {
  25.         this("X");    // 여기 사용된 this는 생성자중 1개의 인자값을 받는 Test02Member(String id)를 바라본다.
  26.         this.pw = pw;
  27.         //this([인자값들]) : 해당생성자의 초기화 코딩을 복제하는 기능
  28.     }
  29.    
  30.     public Test02Member(String id, String pw, String name, String tel) {
  31.         this()//this 생성자, 클래스 내부에서 생성
  32.         num++;
  33.         this.id = id;
  34.         this.pw = pw;
  35.         this.name = name;
  36.         this.tel = tel;
  37.     }
  38.    
  39.    
  40.    
  41.  
  42.  
  43. } // end class




  1. package test.com;
  2.  
  3. public class Test02MemberMain {
  4.  
  5.     public static void main(String[] args) {
  6.         System.out.println("Member...");
  7.        
  8.         Test02Member t01 = new Test02Member();
  9.         System.out.println(t01.num);
  10.         System.out.println(t01.id);
  11.         System.out.println(t01.pw);
  12.         System.out.println(t01.name);
  13.         System.out.println(t01.tel);
  14.         System.out.println("=========");
  15.         Test02Member t00 = new Test02Member("uuuu""hhhhh");
  16.         System.out.println(t00.id);
  17.         System.out.println(t00.pw);
  18.         System.out.println("=========");
  19.         Test02Member t02 = new Test02Member("aaa""bbb""ccc""010-123-456");
  20.         System.out.println(t02.num);
  21.         System.out.println(t02.id);
  22.         System.out.println(t02.pw);
  23.         System.out.println(t02.name);
  24.         System.out.println(t02.tel);
  25.        
  26.  
  27.     }
  28.  
  29. }


728x90