mirror of
https://gitee.com/gaohongy/Java-Book.git
synced 2026-07-16 10:43:51 +00:00
更新接口的主函数
This commit is contained in:
+6
-4
@@ -492,11 +492,13 @@ public class TestEdible {
|
||||
public static void main(String[] args) {
|
||||
Object[] objects = { new Tiger(), new Chicken(), new Apple() };
|
||||
for (int i = 0; i < objects.length; i++) {
|
||||
if (objects[i] instanceof Edible)
|
||||
System.out.println(((Edible) objects[i]).howToEat());
|
||||
|
||||
if (objects[i] instanceof Edible) {
|
||||
Edible e = (Edible) objects[i];
|
||||
System.out.println(e.howToEat());
|
||||
}
|
||||
if (objects[i] instanceof Animal) {
|
||||
System.out.println(((Animal) objects[i]).sound());
|
||||
Animal a = (Animal) objects[i];
|
||||
System.out.println(a.sound());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user