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函数有错误,请高手帮忙标注改正一下,谢谢了 展开
 我来答
茜茜0193
2014-10-24 · TA获得超过206个赞
知道答主
回答量:119
采纳率:33%
帮助的人:113万
展开全部
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"); } } } 你复制过去测试下吧! 追问: 麻烦你解释一下为什么这么做就可以了,我那个为什么会出现错误 ,谢谢了
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式