일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 가변인자
- 추상클래스
- Validations
- Full text
- 페이징
- 스프링
- 상속예제
- Random
- 로또
- 자바 야구게임
- 자바
- 이클립스
- 전체
- IBatis procedure
- 상속
- 형변환
- 다형성
- while
- 다운캐스팅
- angular2
- 25가지 효율적인 sql작성법
- 전체텍스트
- 전자정부
- 단축키
- 야구게임
- 업캐스팅
- Login with OAuth Authentication
- jquery
- full text indexing
Archives
- Today
- Total
nalaolla
오브젝트 예제 3 (class와 class 이용하여 배열처리) 본문
728x90
반응형
- package test.com;
- public class Test03Animal {
- String kind; // 종류
- long count; // 개체수
- int life; // 수명
- int legCount; // 다리수
- Test02Person p;
- public Test03Animal(Test02Person p) {
- this.p = p;
- }
- public Test03Animal() {
- kind = "포유류";
- count = 1000L;
- life = 80;
- legCount = 4;
- }
- public Test03Animal(String kind, long count, int life, int legCount) {
- this.kind = kind;
- this.count = count;
- this.life = life;
- this.legCount = legCount;
- }
- }
- package test.com;
- public class Test02Person {
- int height;
- int weight;
- String location;
- String[] friends;
- public Test02Person() {
- height = 187;
- weight = 80;
- location = "Guri";
- friends = new String[3];
- friends[0] = "AA";
- friends[1] = "BB";
- friends[2] = "CC";
- }
- public Test02Person(int height, int weight, String location) {
- this.height = height;
- this.weight = weight;
- this.location = location;
- friends = new String[]{"CHOI", "HAN"};
- }
- public Test02Person(int height, int weight, String location, String[] friends) {
- this.height = height;
- this.weight = weight;
- this.location = location;
- this.friends = friends;
- }
- }
- package test.com;
- public class Test03AnimalMain {
- public static void main(String[] args) {
- System.out.println("Animal...");
- Test03Animal a01 = new Test03Animal();
- System.out.println(a01.kind);
- System.out.println(a01.count + "마리");
- System.out.println("수명 : " + a01.life);
- System.out.println("다리수 : " + a01.legCount);
- Test03Animal a02 = new Test03Animal("곤충", 3L, 1, 6);
- System.out.println(a02.kind);
- System.out.println(a02.count);
- System.out.println(a02.life);
- System.out.println(a02.legCount);
- System.out.println("======================");
- /*
- Test02Person p = new Test02Person();
- Test03Animal a03 = new Test03Animal(p);
- */
- //Test03Animal a03 = new Test03Animal(new Test02Person());
- Test03Animal a03 = new Test03Animal(new Test02Person(155,23,"seoul"));
- System.out.println(a03.p);
- System.out.println(a03.p.height);
- System.out.println(a03.p.weight);
- System.out.println(a03.p.location);
- System.out.println(a03.p.friends.length);
- System.out.println("======================");
- //a01과 a02를 배열로 만들어서 출력
- Test03Animal[] as = new Test03Animal[2];
- as[0] = a01;
- as[1] = a02;
- for (int i = 0; i < as.length; i++) {
- //System.out.println(as[i]);
- System.out.println(as[i].kind);
- System.out.println(as[i].count);
- System.out.println(as[i].life);
- System.out.println(as[i].legCount);
- System.out.println();
- }
- System.out.println("======================");
- }
- }
728x90
반응형
'JAVA > 6. Object2' 카테고리의 다른 글
오브젝트 예제 5 (0) | 2016.06.22 |
---|---|
오브젝트 예제 2 (배열활용) (0) | 2016.06.22 |
오브젝트 예제 1 (0) | 2016.06.22 |