动态绑定做为Java多态特性的一个重要实现html
有如下三个类:java
public class Fruit { public static final String TAG = "Fruit"; public Fruit() { } public void say() { Log.i(TAG, "say: i am fruit: "); } }
public class Apple extends Fruit{ public void say(){ Log.i(TAG, "say: i am apple"); } }
public class Orange extends Fruit { public void say(){ Log.i(TAG, "say: i am orange"); } }
其中Apple和Orange类都继承Fruit类。app
而后在使用的时候ui
Fruit fruit = new Apple(); fruit.say();
咱们都知道最后打印的结果是debug
debug I/Fruit: say: i am apple
很明显这就是Java多态特性的体现。日志
咱们明明调用的是Fruit对象的方法。可是运行时却调用了Apple对象的方法,这是怎么实现的呢?这就涉及到了Java的动态绑定了。code
这里肯定两个概念:编译时类型与运行时类型htm
编译时类型就是指改对象在编译后的文件里的类型也就是该对象声明时的类型,而运行时类型是指在程序运行时动态指定的类型也就是该对象定义时的类型。若是编译时类型与运行时类型不一致就会发生运行时动态绑定。对象
好比上面定义的Fruit fruit = new Apple();blog
fruit的编译时类型是Fruit。
咱们能够从编译后的class文件看出
#4 = Class #243 // com/meiliwu/dragon/model/Apple #5 = Methodref #4.#241 // com/meiliwu/dragon/model/Apple."<init>":()V #6 = Methodref #244.#245 // com/meiliwu/dragon/model/Fruit.say:()V
咱们能够从编译后的文件看出。fruit.say方法在编译后指向的是Fruit的say方法。可是运行时类型是Apple。运行时倒是调用Apple的say方法。咱们从日志能够看出来。
若是咱们将代码改为这样
Apple fruit = new Apple(); fruit.say();
打印日志以下:
12-25 11:25:17.803 9709-9709/com.meiliwu.dragon.debug I/Fruit: say: i am apple
咱们再看看编译后的文件
4 = Class #243 // com/meiliwu/dragon/model/Apple #5 = Methodref #4.#241 // com/meiliwu/dragon/model/Apple."<init>":()V #6 = Methodref #4.#244 // com/meiliwu/dragon/model/Apple.say:()V
从代码能够看出编译时类型与运行时类型一致,这是不会发生动态绑定的,这时能够从编译后的class文件得出验证。
PECS指“Producer Extends,Consumer Super”
好比:
List<? extends Fruit> fruitList = new ArrayList<>(); fruitList.add(new Apple()); fruitList.add(new Orange());
可是编译器却报了编译错误:
按理说fruitList是一个持有类型为Fruit及其子类的泛型列表啊,为何不能往其中添加Fruit的子类呢?
由于泛型的一大好处就是能够在编译时检查,避免传入不相符的类型可能致使的ClassCastException了。可是声明fruitList的时候没有明确的指定泛型的具体类型,因此编译器没法确认其持有的具体类型,固然也就拒绝了add操做。
fruitList只是规定了泛型的上限,可是并无肯定具体的类型,也没法肯定具体的子类型,能够是Apple,Orange还多是Banana,因此不能把具体的对象添加进去,否则使用的时候可能致使ClassCastException了。可是能够保证从里面取出来的数据都是Fruit及其子类,并且仍是Fruit的某一个子类。
咱们把代码改为下面这样子就能够添加不一样的Fruit对象了。
由于咱们规定了fruitList持有的都是Fruit及其父类,能够将Fruit 及其子类都添加进去
List<? super Fruit> fruitList = new ArrayList<>(); fruitList.add(new Apple()); fruitList.add(new Orange());
可是
fruitList.add(new Object());
却不行,why?由于在编译时没法确认具体的fruitList持有的是Fruit的哪个父类,要想肯定就必须指定具体泛型类了。因此就没法往里面写入Fruit的父类型对象。
因此咱们能够从Java Collctions copy方法的签名能够看出
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
dest集合持有?super T ,能够往里面写入全部T及其子类对象,而src集合持有? extends T泛型。能够确保的是从里面读取的数据都是T及其子类。因此能够写入dest了。
PS:Java 类的final方法和static不能复写