-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimalTest.java
More file actions
77 lines (58 loc) · 1.67 KB
/
Copy pathAnimalTest.java
File metadata and controls
77 lines (58 loc) · 1.67 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package oopcore3_downcasting;
import java.lang.reflect.Array;
import java.util.ArrayList;
class Animal{
public void move() {
System.out.println("동물이 움직입니다.");
}
}
class Human extends Animal{
@Override
public void move() {
System.out.println("사람이 두 발로 걷습니다.");
}
public void readBook() {
System.out.println("사람이 책을 읽습니다.");
}
}
class Tiger extends Animal{
@Override
public void move() {
System.out.println("호랑이가 네 발로 뜁니다.");
}
public void hunting() {
System.out.println("호랑이가 사냥을 합니다.");
}
}
public class AnimalTest {
public static void main(String[] args) {
Animal hAnimal = new Human();
Animal tAnimal = new Tiger();
AnimalTest test = new AnimalTest();
test.moveAnimal(hAnimal);
test.moveAnimal(tAnimal);
ArrayList<Animal> animalList = new ArrayList<>();
animalList.add(hAnimal);
animalList.add(tAnimal);
for(Animal animal : animalList) {
animal.move();
}
test.testDownCasting(animalList);
}
public void testDownCasting(ArrayList<Animal> list) {
for(int i=0; i<list.size(); i++) {
Animal animal = list.get(i);
if(animal instanceof Human) {
Human human = (Human)animal;
human.readBook();
}
else if(animal instanceof Tiger) {
Tiger tiger = (Tiger)animal;
tiger.hunting();
}
}
}
public void moveAnimal(Animal animal) {
animal.move();
}
}