관리 메뉴

nalaolla

오브젝트 예제 5 본문

JAVA/6. Object2

오브젝트 예제 5

날아올라↗↗ 2016. 6. 22. 17:14
728x90
  1. package test.com;
  2.  
  3. public class Test04Car {
  4.    
  5.     String  kind;
  6.     int  year;
  7.     String  num;
  8.     int  doorCount;
  9.    
  10.     public Test04Car() {
  11.         kind = "승용차";
  12.         year = 2015;
  13.         num = "11가1234";
  14.         doorCount = 4;
  15.     }
  16.    
  17.     public Test04Car(String kind, int year, String num, int doorCount) {
  18.         this.kind = kind;
  19.         this.year = year;
  20.         this.num = num;
  21.         this.doorCount = doorCount;
  22.     }
  23. }


  1. package test.com;
  2.  
  3. public class Test05BBB {
  4.     Test04Car   c;
  5.     Test04Car[] cs;
  6.    
  7.     public Test05BBB() {
  8.         c = new Test04Car();
  9.         cs = new Test04Car[3];
  10.         cs[0] = new Test04Car();
  11.         cs[1] = new Test04Car("aaa"1111"bbbb"22);
  12.         cs[2] = new Test04Car("ccc"444"ddd"555);
  13.     }
  14.    
  15.     public Test05BBB(Test04Car c, Test04Car[] cs) {
  16.         this.c = c;
  17.         this.cs = cs;
  18.     }
  19.    
  20.     public Test05BBB(Test04Car[] cs) {
  21.         this.cs = cs;
  22.         for (Test04Car c : cs) {
  23.            
  24.             System.out.print(c.kind + " ");
  25.             System.out.print(c.year + " ");
  26.             System.out.print(c.num + " ");
  27.             System.out.println(c.doorCount);
  28.         }
  29.     }
  30.    
  31.     public Test05BBB(Test04Car c) {
  32.         this.c = c;  
  33.     }
  34. }




  1. package test.com;
  2.  
  3. public class Test04CarMain {
  4.  
  5.     public static void main(String[] args) {
  6.         System.out.println("Car...");
  7.         String line = "================================";
  8.        
  9.         Test04Car c01 = new Test04Car();
  10.         System.out.println(c01.kind);
  11.         System.out.println(c01.year);
  12.         System.out.println(c01.num);
  13.         System.out.println(c01.doorCount);
  14.        
  15.         System.out.println(line);
  16.         Test04Car c02 = new Test04Car("승합"2012"33삼3333"3);
  17.         System.out.println(c02);
  18.         System.out.println(c02.kind);
  19.         System.out.println(c02.year);
  20.         System.out.println(c02.num);
  21.         System.out.println(c02.doorCount);
  22.        
  23.         System.out.println(line);
  24.         //c01, c02를 이용하여 배열생성 및 출력
  25.        
  26.         Test04Car[] cs = new Test04Car[]{c01, c02};
  27.        
  28.         System.out.println("차종 연식 차번호 도어수");
  29.         /*
  30.         for (int i = 0; i < cs.length; i++) {
  31.             //System.out.println(cs[i]);
  32.             System.out.print(cs[i].kind + " ");
  33.             System.out.print(cs[i].year + " ");
  34.             System.out.print(cs[i].num + " ");
  35.             System.out.print(cs[i].doorCount + " ");
  36.            
  37.             System.out.println();
  38.         }*/
  39.        
  40.         for (Test04Car c : cs) {
  41.             System.out.print(c.kind + " ");
  42.             System.out.print(c.year + " ");
  43.             System.out.print(c.num + " ");
  44.             System.out.println(c.doorCount);
  45.            
  46.         }
  47.        
  48.         System.out.println("=========== Test05BBB =============");
  49.         Test05BBB b01 = new Test05BBB();
  50.         System.out.print(b01.c.kind);
  51.         System.out.print(b01.c.year + " ");
  52.         System.out.print(b01.c.num + " ");
  53.         System.out.println(b01.c.doorCount);
  54.        
  55.         System.out.println(b01.cs.length);
  56.         for (int i = 0; i < b01.cs.length; i++) {
  57.             System.out.println(b01.cs[i]);
  58.             System.out.println(b01.cs[i].kind);
  59.             System.out.println(b01.cs[i].year);
  60.             System.out.println(b01.cs[i].num);
  61.             System.out.println(b01.cs[i].doorCount);
  62.         }
  63.        
  64.         System.out.println("=========== Test05BBB =============");
  65.         Test05BBB b02 = new Test05BBB(cs);
  66.         System.out.println(b02);
  67.     } //end main
  68.  
  69. }   //end class


728x90

'JAVA > 6. Object2' 카테고리의 다른 글

오브젝트 예제 3 (class와 class 이용하여 배열처리)  (0) 2016.06.22
오브젝트 예제 2 (배열활용)  (0) 2016.06.22
오브젝트 예제 1  (0) 2016.06.22