public class TestEdible {
public static void main(String[] args) {
Object[] objects = { new Chicken(), new Apple()};
for (int i = 0; i < objects.length; i++) {
if (objects[i] instanceof Edible)
System.out.println((Edible)objects[i].howToEat());
}
}
}
interface Edible {
public abstract String howToEat();
}
class Animal {
}
class Chicken extends Animal implements Edible {
public String howToEat() {
return "Chicken : Fry it";
}
}
class Tiger extends Animal {
}
abstract class Fruit implements Edible {
}
class Apple extends Fruit {
public String howToEat() {
return "Apple: Make apple cider";
}
}
class Orange extends Fruit {
public String howToEat() {
return "orange : Make orange juice";
}
}
------解决方案--------------------
System.out.println((Edible)objects[i].howToEat());改成System.out.println(((Edible)objects[i]).howToEat());
同样的帖子没必要在每个版块都发一次。。在大版能看到就Ok