- 实现多态的技术称为:动态绑定,是指在执行期间(不是编译期间)判断所引用对象的实际类型,根据其实际的类型调用其相应的方法。
多态的三个条件:
1.要有继承
2.要有重写
3.父类引用指向子类对象
class Animal{
public String name;
Animal(){
}
Animal(String name){
this.name=name;
}
public void enjoy()
{
System.out.println(",,,");
}
}
class Bird extends Animal{
private String featherColor;
Bird(String n,String f)
{
super(n);
featherColor=f;
}
public void enjoy()
{
System.out.println("鸟鸣");
}
}
class Cat extends Animal{
public void enjoy()
{
System.out.println("猫叫");
}
}
public class DuoTai
{
public static void main(String args[])
{
Animal a=new Bird("xiaobai","red");
a.enjoy();
Animal b=new Cat();
b.enjoy();
}
}