首页 新闻 会员 周边

java多态

0
[待解决问题]

package my;

interface Animal{
void shout();
}
public static void animalshout(Animal an) { //为什么这行的void通不过编译啊?
an.shout();
}
class Cat implements Animal{
public void shout() {
System.out.println("喵喵喵");
}
}
class Dog implements Animal{
public void shout() {
System.out.println("汪汪汪");
}
}
class yichang {
public static void main(String[] args) {
Animal an1=new Dog();
Animal an2=new Cat();
animalshout(an1);
animalshout(an2);
}
}
有大佬能告诉我一下问题在哪吗?

老萌新51的主页 老萌新51 | 菜鸟二级 | 园豆:210
提问于:2019-11-17 21:24
< >
分享
所有回答(1)
0

因为方法应该写到类里去

yytxdy | 园豆:1680 (小虾三级) | 2019-11-17 21:29

懂了 谢谢 看方法的时候以为跟c一样就没怎么认真看

支持(0) 反对(0) 老萌新51 | 园豆:210 (菜鸟二级) | 2019-11-17 21:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册