//sorry 第一个发错了.
class Nine2 {
public static void main(String[] args)
{
fun(new cat("猫","瞄")); //构造函数形参
fun(new dog("狗","汪"));//构造函数形参
}
public static void fun(animal a)
{
System.out.println(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);
}
}
[/code] |