找不到符号
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());
------解决方案--------------------没有进行向下类型转化
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()); //向下类型转化
}
}
}
------解决方案--------------------System.out.println((Edible)objects[i].howToEat());改成System.out.println(((Edible)objects[i]).howToEat());正解