动态绑定和多态对于程序的可扩展性起着很大的作用 多态是指一个程序中功能方法名字相同但实现结果不同的情况。 一种多态是通过子类对超类或父类方法的重写实现 另一种多态是通过在一类中定义同名方法参数不同的重载实现。 以下是重写父类方法例子说明:
public class Animal { public String name; protected int r = 10; Animal(String name) { this.name = name; } void enjoy() { System.out.println("动物叫。。。。"); } } public class Cat extends Animal { public String eyecolor; Cat(String name, String c) { super(name); this.eyecolor = c; } void enjoy() { System.out.println("猫叫。。。。"); } } public class Lady { public String name; public Animal Animal; Lady(String n, Animal animal) { this.name = n; this.Animal = animal; } public void sound() { Animal.enjoy(); } public static void main(String[] args) { Cat q = new Cat("q","blue"); Lady x = new Lady("x",q); x.sound(); } }