本帖最后由 凝聚 于 2013-11-16 20:05 编辑
package test;
public class Nine2 {
public static void main(String[] args) {
fun(new cat());
}
public static void fun(animal a)
{
a.jiao();
}
}
abstract class animal
{
String name;
String call;
public abstract String jiao();
}
class cat extends animal
{
public cat(String name,String call)
{
this.name=name;
this.call=call;
}
public String jiao()
{
return this.name+this.call;
//System.out.println(this.name+this.call);
}
}
class dog extends animal
{
public dog(String name,String call)
{
this.name=name;
this.call=call;
}
public String jiao()
{
return this.name+this.call;
//System.out.println(this.name+this.call);
}
}
我这段代码中 fun(new cat());这句话有错误“构造函数的猫()是未定义的”那么该怎么改啊!如果这样Animal an=new Cat("猫","喵~喵!");
System.out.println(an.talk());那么动物种类多了就显得有些麻烦了?请问该怎么改啊?
|
|