新手请教一个编程错误
麻烦各位高手解答一下,下面这个程序运行时出错。请问是哪里错了呀? 麻烦知道告诉一下 谢谢了!!!class Animal {
public String name;
Animal(String name) {
this.name = name;
}
}
class Cat extends Animal {
public String eyescolor;
Cat(String n, String c) {
super(n);
eyescolor = c;
}
}
class Dog extends Animal {
public String furcolor;
Dog(String n, String c) {
super(n);
furcolor = c;
}
}
public class AnimalTest {
/*
public static void main (String args[]) {
Animal a = new Animal("name");
Cat c = new Cat("catname","blue");
Dog d = new Dog("dogname","black");
System.out.println(a instanceof Animal);
System.out.println(c instanceof Animal);
System.out.println(d instanceof Animal);
System.out.println(c instanceof Cat);
a = new Dog("bigyellow","yellow");
System.out.println(a.name);
//System.out.println(a.furname);
System.out.println(a instanceof Animal);
System.out.println(a instanceof Dog);
Dog d1 = (Dog)a;
System.out.println(d1.furcolor);
}
*/
public static void main(String args[]) {
Animal a = new Animal("name");
Cat c = new Cat("catname","blue");
Dog d = new Dog("dogname","black");
AnimalTest.f(a);
AnimalTest.f(c);
AnimalTest.f(d);
public void f(Animal n) {
System.out.println("name: "+n.name);
if(n instanceof Cat) {
Cat c1 = (Cat)n;
System.out.println("eyescolor: "+c1.eyescolor);
}
else if(n instanceof Dog) {
Dog d1 = (Dog)n;
System.out.println("furcolor: "+d1.furcolor);
}
}
}
}
[此贴子已经被作者于2016-12-30 21:24编辑过]