注册 登录
编程论坛 JAVA论坛

新手请教一个编程错误

annie08 发布于 2016-12-30 21:23, 1786 次点击
麻烦各位高手解答一下,下面这个程序运行时出错。请问是哪里错了呀?  麻烦知道告诉一下  谢谢了!!!

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

4 回复
#2
枫xby2016-12-31 12:55
你在静态方法 (main())里写了 非静态方法(f()) 当然会报错, 你把 f()这个方法写到 main()方法外面,因为你是类名.方法名调用的 f(),所以你要在f()这个方法前 加 static 方法修饰
#3
枫xby2016-12-31 12:57
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);
        }
    }


}

这是我帮你修改后的代码你看下
#4
annie082016-12-31 15:20
嗯  可以了  非常感谢前辈的指导!!
#5
kaosaier2016-12-31 17:40
回复 3楼 枫xby
二楼正话
1