读书人

JAVA步骤中Bridge修饰符

发布时间: 2012-10-11 10:16:10 作者: rapoo

JAVA方法中Bridge修饰符

??? 前段时间研究spring中的autowireByType实现原理时,看到了一个方法修饰符bridge,由于以前没有见过这个修饰符,不知道它表示的具体含义,后面的代码也就看不明白了,原理研究也就搁在那里了,没什么进展。恰好这几天在阅读《JAVA泛型与集合》这本书时,看到了书中有对这个修饰符的讲解,才稍微有点眉目。原来这个修饰符不是给程序员使用的,而是编译器为了实现泛型而自动产生的。书中描述如下:

?????? As we mentioned earlier, generics are implemented by erasure: when you write code
with generics, it compiles in almost exactly the same way as the code you would have
written without generics. In the case of a parameterized interface such as Compara
ble<T>, this may cause additional methods to be inserted by the compiler; these additional
methods are called bridges.

??

???? 下面举个列子解释一下这段话的意思。

?????

?????在JDK1.5之前,是不存在泛型的,对象需要比较时需要实现Comparable接口,此时接口如下:

public int Integer.compareTo(Integer)public bridge int Integer.compareTo(java.lang.Object)

?

??

???

读书人网 >编程

热点推荐