| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1558 人关注过本帖
标题:新手请教一个编程错误
取消只看楼主 加入收藏
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
快速回复:新手请教一个编程错误
数据加载中...
 
   



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

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