java 复习笔记——多态性
1)
通过覆盖父类的方法来实现,在运行时根据传递的对象引用,来调用相应的方法。
2)
多态须具备:
1. 基类 和各个子类
2. 基类 引用, 指向实例化的子类对象.
3)
class Animal
{
int height,weight;
void eat()
{
System.out.println("animal eat");
}
void sleep()
{
System.out.println("animal sleep");
}
void breathe()
{
System.out.println("animal breathe");
}
}
class Fish extends Animal
{
int height;
void breathe()
{
System.out.println("fish bubble");
}
}
class Integration
{
static void fn(Animal an)
{
an.breathe();
}
public static void main(String[] args)
{
Fish fh=new Fish();
Animal an;
an=fh;
Integration.fn(an);//或an.breathe();
}
}