관리 메뉴

nalaolla

오브젝트 예제 3 (class와 class 이용하여 배열처리) 본문

JAVA/6. Object2

오브젝트 예제 3 (class와 class 이용하여 배열처리)

날아올라↗↗ 2016. 6. 22. 17:13
728x90
  1. package test.com;
  2.  
  3. public class Test03Animal {
  4.  
  5.     String kind; // 종류
  6.     long count; // 개체수
  7.     int life; // 수명
  8.     int legCount; // 다리수
  9.    
  10.     Test02Person p;
  11.    
  12.     public Test03Animal(Test02Person p) {
  13.         this.p = p;
  14.     }
  15.  
  16.     public Test03Animal() {
  17.         kind = "포유류";
  18.         count = 1000L;
  19.         life = 80;
  20.         legCount = 4;
  21.     }
  22.  
  23.     public Test03Animal(String kind, long count, int life, int legCount) {
  24.         this.kind = kind;
  25.         this.count = count;
  26.         this.life = life;
  27.         this.legCount = legCount;
  28.     }
  29.  
  30. }




  1. package test.com;
  2.  
  3. public class Test02Person {
  4.  
  5.     int height;
  6.     int weight;
  7.     String location;
  8.     String[] friends;
  9.  
  10.     public Test02Person() {
  11.         height = 187;
  12.         weight = 80;
  13.         location = "Guri";
  14.         friends = new String[3];
  15.         friends[0] = "AA";
  16.         friends[1] = "BB";
  17.         friends[2] = "CC";
  18.     }
  19.  
  20.     public Test02Person(int height, int weight, String location) {
  21.         this.height = height;
  22.         this.weight = weight;
  23.         this.location = location;
  24.         friends = new String[]{"CHOI""HAN"};
  25.     }
  26.  
  27.     public Test02Person(int height, int weight, String location, String[] friends) {
  28.         this.height = height;
  29.         this.weight = weight;
  30.         this.location = location;
  31.         this.friends = friends;
  32.     }
  33.  
  34. }





  1. package test.com;
  2.  
  3. public class Test03AnimalMain {
  4.  
  5.     public static void main(String[] args) {
  6.         System.out.println("Animal...");
  7.        
  8.         Test03Animal a01 = new Test03Animal();
  9.         System.out.println(a01.kind);
  10.         System.out.println(a01.count + "마리");
  11.         System.out.println("수명 : " + a01.life);
  12.         System.out.println("다리수 : " + a01.legCount);
  13.        
  14.         Test03Animal a02 = new Test03Animal("곤충", 3L, 16);
  15.         System.out.println(a02.kind);
  16.         System.out.println(a02.count);
  17.         System.out.println(a02.life);
  18.         System.out.println(a02.legCount);
  19.        
  20.         System.out.println("======================");
  21.        
  22.         /*
  23.         Test02Person p = new Test02Person();
  24.         Test03Animal a03 = new Test03Animal(p);
  25.         */
  26.        
  27.         //Test03Animal a03 = new Test03Animal(new Test02Person());
  28.         Test03Animal a03 = new Test03Animal(new Test02Person(155,23,"seoul"));
  29.         System.out.println(a03.p);
  30.         System.out.println(a03.p.height);
  31.         System.out.println(a03.p.weight);
  32.         System.out.println(a03.p.location);
  33.         System.out.println(a03.p.friends.length);
  34.        
  35.        
  36.         System.out.println("======================");
  37.         //a01과 a02를 배열로 만들어서 출력
  38.        
  39.         Test03Animal[] as = new Test03Animal[2];
  40.         as[0] = a01;
  41.         as[1] = a02;
  42.        
  43.         for (int i = 0; i < as.length; i++) {
  44.             //System.out.println(as[i]);
  45.             System.out.println(as[i].kind);
  46.             System.out.println(as[i].count);
  47.             System.out.println(as[i].life);
  48.             System.out.println(as[i].legCount);
  49.             System.out.println();
  50.         }
  51.        
  52.        
  53.         System.out.println("======================");
  54.        
  55.        
  56.        
  57.        
  58.     }
  59.  
  60. }


728x90

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

오브젝트 예제 5  (0) 2016.06.22
오브젝트 예제 2 (배열활용)  (0) 2016.06.22
오브젝트 예제 1  (0) 2016.06.22