标题:新手请教一个编程错误
取消只看楼主
annie08
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-12-30
结帖率:100%
已结贴  问题点数:20 回复次数:1 
新手请教一个编程错误
麻烦各位高手解答一下,下面这个程序运行时出错。请问是哪里错了呀?  麻烦知道告诉一下  谢谢了!!!

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编辑过]

搜索更多相关主题的帖子: super public 
2016-12-30 21:23
annie08
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-12-30
得分:0 
嗯  可以了  非常感谢前辈的指导!!
2016-12-31 15:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-473153-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.223026 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved