学员冯伟立今天午时问了我一个有趣的Java编译问题,我也无法给他诠释,不知道有没有路过的高人能够诠释清嚣张原因,望不惜赐教!8 s8 c; z0 ?9 R1 h* _
下面轨范的main体例中的第二行代码和注释中的两行代码表达的意思完全不异,注释中的两行代码不能经由过程编译(这很等闲理解),而第二行(采用体例挪用链)却可以顺遂经由过程编译(这就很难理解了)。' i, i1 | ]: u
public class Test. f" n* y$ W& k
public static void main throws Exception: q6 ?3 x- G' D0 A
}
! D4 [- w. D5 I$ y7 H( h# F 感谢感动paulex师长教师的辅佐,在paulex师长教师的提醒下,我根基上年夜白了上述问题的原因。下面是paulex师长教师的解答:! r& H3 H. N2 \ B' s. j6 ^
因为Generic, 编译器可以在编译期获得类型所以可以编译这类代码。你将下面那两行改成
" u3 i w6 y5 A& S! |! | Class? extends Test c = obj).getClass;
3 x$ s8 r3 f1 A/ j; d/ | X c.newInstance.func;& n. O9 ~( o7 J- h
应该就能经由过程编译了。
" b6 f0 N J% r( M N0 D% k 下面是我在paulex师长教师解答的基本上,对问题的进一步诠释: h8 X# s- X; @( m8 o
在JDK 1.5中惹人范型后,Object.getClass体例的界说如下:9 e, w" {- b# ?6 |- O1 ?8 t
public final Class? extends Object getClass& l/ R" t& y, X8 E/ _- C: f
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.+ k! m& S9 y# l# a
Returns:
2 u' g. T" L( O0 c/ }- k0 K$ b, X; K The Java.lang.Class object that represents the runtime class of the object. The result is of type Class? extends X where X is the erasure of the static type of the expression on which getClass is called.2 y, s4 X& \$ i
这声名obj).getClass语句返回的对象类型为Class? extends Test,而ClassT的newInstance体例的界说如下:9 A! ^" Z4 Y Q7 p
public T newInstance throws InstantiationException,IllegalACCESSException
N1 S3 a8 c! E; u' @ 即对于编译器看来,ClassTest的newInstance体例的对象类型为Test,而obj).getClass返回的为对象类型为Class? extends Test,所以,编译器认为obj).getClass.newInstance返回的对象类型为Test。
# M3 k* J7 j$ V6 n" C8 A2 g7 W 下面这两行代码之所以无法经由过程编译
* ~5 }5 }( e, U, Q) n( r Class c = obj).getClass;
' j9 R+ D/ S1 L! t6 s c.newInstance.func;
, W8 J0 ~7 N6 j# t. ~+ c 是因为obj).getClass返回的为对象类型为Class? extends Test,可是我们在第一行将结不美观强制转换成了Class,然后再去挪用Class的newInstance体例,而不是去挪用ClassTest的newInstance体例,编译器当然不再认为Class的newInstance体例返回的对象为Test了。 |