| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1610 人关注过本帖
标题:新手请教一个编程错误
只看楼主 加入收藏
annie08
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-12-30
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
新手请教一个编程错误
麻烦各位高手解答一下,下面这个程序运行时出错。请问是哪里错了呀?  麻烦知道告诉一下  谢谢了!!!

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
枫xby
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:82
专家分:371
注 册:2014-10-19
收藏
得分:20 
你在静态方法 (main())里写了 非静态方法(f()) 当然会报错, 你把 f()这个方法写到 main()方法外面,因为你是类名.方法名调用的 f(),所以你要在f()这个方法前 加 static 方法修饰
2016-12-31 12:55
枫xby
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:82
专家分:371
注 册:2014-10-19
收藏
得分:0 
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 static 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-31 12:57
annie08
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-12-30
收藏
得分:0 
  可以了  非常感谢前辈的指导!!
2016-12-31 15:20
kaosaier
Rank: 5Rank: 5
等 级:职业侠客
威 望:7
帖 子:124
专家分:303
注 册:2016-7-23
收藏
得分:0 
回复 3楼 枫xby
二楼正话

minecraft,我的最爱!
2016-12-31 17:40
快速回复:新手请教一个编程错误
数据加载中...
 
   



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

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