提高了扩展性,但是只能使用父类的引用访问父类中的成员。有点蒙圈了希望大神给个细致点的解析。
abstract class Animal
{
public abstract void eat();
}
class cat extends Animal
{
public void eat()
{
System.out.println("吃鱼");
}
publci catchMouse()
{
System.out.println("抓老鼠");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("吃骨头");
}
public void kanJia()
{
System.out.println("看家");
}
}
class Pig extends Animal
{
public void eat()
{
System.out.println("吃骨头");
}
public void gongDi()
{
System.out.println("拱地");
}
}
class DuoTaiDemo
{
public static void main(String[] args)
{
//Cat c = new Cat();
//c.eat();
//Dog d = new Dog();
//d.eat();
function(new Cat());
function(new Dog());
function(new Pig());
}
public static void function(Animal a)// Animal a = new Cat();
{
a.eat();
}
} |
|