java 代码提示非法的表达式开始
classAnimal{publicStringname;Animal(Stringname){this.name=name;}}classCatextendsAnima...
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 TestAnimal { public static void main (String args[]) { TestAnimal test = new TestAnimal(); Animal a =new Animal("name"); Cat c = new Cat("Catname", "blue"); Dog d = new Dog("dogname","black"); test.f(a); test.f(c); test.f(d); public void f(Animal a) { System.out.println("name: " + a.name); if (a instanceof Cat) { Cat cat = (Cat)a; System.out.println(" "+ cat.eyesColor + " eyse"); } else if(a instanceof Dog) { Dog dog = (Dog)a; System.out.println(" "+ dog.furColor + " fur"); } } }} 编译时提示f函数有错误,请高手帮忙标注改正一下,谢谢了
展开
1个回答
展开全部
package test; 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 TestAnimal { public static void main (String args[]) { TestAnimal test = new TestAnimal(); Animal a =new Animal("name"); Cat c = new Cat("Catname", "blue"); Dog d = new Dog("dogname","black"); test.f(a); test.f(c); test.f(d); } public void f(Animal a) { System.out.println("name: " + a.name); if (a instanceof Cat) { Cat cat = (Cat)a; System.out.println(" "+ cat.eyesColor + " eyse"); }else if(a instanceof Dog) { Dog dog = (Dog)a; System.out.println(" "+ dog.furColor + " fur"); } } } 你复制过去测试下吧! 追问: 麻烦你解释一下为什么这么做就可以了,我那个为什么会出现错误 ,谢谢了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询