abstract class Animal
{ String name;
abstract void eat(String name,String food);
}
/*class Eat
{
public static void starteat(Animal a,String name,String food)
{
a.eat(name,food);}*/
class Dog extends Animal
{
public void eat(String name,String food)
{
System.out.println(name+"吃骨"+food);
}
public void lookHome()
{
System.out.println(name+"看家");
}
}
class Pig extends Animal
{
public void eat(String name,String food)
{
System.out.println(name+"吃"+food);
}
public void GongDi()
{
System.out.println(name+"在拱地");
}
}
class DuoTaiDemo2
{
public static void main(String[] args)
{
Animal a=new Pig();
a.name="哼哼";
Eat.strateat(a,a.name,"草");
//EAT.eatt(a,a.name,"草");
}
}
我如果把注释的语句改为class EAT
class{
public static void eatt(Animal a,String name,String food)
{
if(a instanceof Animal)
{
a.eat(name,food);
}
}
}
请问这两者最大的区别在哪里 ,哪个程序更优化呢 |