注册 登录
编程论坛 JAVA论坛

自学java,在其多态方面的一个问题

我不会喜欢你 发布于 2016-04-08 23:16, 2896 次点击
程序代码:
public class polymorphicDemo{
    public static void main(String [] args){
        Animal animal=new Animal("动物");
        Animal c=new Cat("招财猫","黑黄色");
        Animal d=new Dog("来福","黑色");
        System.out.println(animal.getName());
        System.out.println(c.GetEyesColor());
        System.out.println(d.getFurColor());
        System.out.println(animal instanceof Animal);
        System.out.println(c instanceof Animal);
        System.out.println(d instanceof Animal);
        System.out.println(animal instanceof Cat);
    }
}
class Animal{
    private String name;
    public Animal(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
}
class Cat extends Animal{
    private String eyesColor;
    public Cat(String name,String eyesColor){
        super(name);
        this.eyesColor=eyesColor;
    }
    public String GetEyesColor(){
        return eyesColor+"的"+super.getName();
    }
}
class Dog extends Animal{
    private String furColor;
    public Dog(String name,String furColor){
        super(name);
        this.furColor=furColor;
    }
    public String getFurColor(){
        return furColor+"的"+super.getName();
    }
}


报的错:
---------- 编译 ----------
polymorphicDemo.java:7: 错误: 找不到符号
        System.out.println(c.GetEyesColor());
                            ^
  符号:   方法 GetEyesColor()
  位置: 类型为Animal的变量 c
polymorphicDemo.java:8: 错误: 找不到符号
        System.out.println(d.getFurColor());
                            ^
  符号:   方法 getFurColor()
  位置: 类型为Animal的变量 d
2 个错误

为什么会错呀?

4 回复
#2
诸葛欧阳2016-04-09 10:45
把子类定义成父类,子类中特有的方法用不了的
#3
alice_usnet2016-04-09 13:55
c,d是上转型对象,已经丢失了子类新增的变量和方法。
#4
我不会喜欢你2016-04-09 22:13
回复 2楼 诸葛欧阳
谢谢,我知道了
#5
我不会喜欢你2016-04-09 22:13
回复 3楼 alice_usnet
谢谢,我知道了
1