Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
59 lines (42 loc) · 1.38 KB
/
Copy pathMain.java
File metadata and controls
59 lines (42 loc) · 1.38 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
package interfaces;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Cat oneCat = new Cat();
Dog twoDog = new Dog();
Bird threeBird = new Bird();
Fish fourFish = new Fish();
ArrayList<Animal> animals = new ArrayList<>();
animals.add(oneCat);
animals.add(twoDog);
animals.add(threeBird);
animals.add(fourFish);
for (Animal animal : animals) {
animal.eat();
}
System.out.println("----------");
Animal oneAnimal = new Dog();
oneAnimal.eat();
Dog dog = (Dog) oneAnimal;
dog.run();
System.out.println("----------");
Cat cat1 = new Cat();
Dog dog1 = new Dog();
Bird bird1 = new Bird();
ArrayList<AbleToRun> runAnimals = new ArrayList<>(); //тип имя интерфейса
runAnimals.add(cat1);
runAnimals.add(dog1);
runAnimals.add(bird1);
for (AbleToRun runny : runAnimals) {
runny.run();
}
System.out.println("----------");
Bird bird2 = new Bird();
ArrayList<AbleToFly> flyAnimals = new ArrayList<>(); //тип имя интерфейса
flyAnimals.add(bird2);
for (AbleToFly flyly : flyAnimals) {
flyly.fly();
}
System.out.println("----------");
}
}