学员冯伟立今天午时问了我一个有趣的Java编译问题,我也无法给他诠释,不知道有没有路过的高人能够诠释清嚣张原因,望不惜赐教!$ _1 Y% p& w5 E: ?' m
下面轨范的main体例中的第二行代码和注释中的两行代码表达的意思完全不异,注释中的两行代码不能经由过程编译(这很等闲理解),而第二行(采用体例挪用链)却可以顺遂经由过程编译(这就很难理解了)。
$ M% Q8 s2 o4 R3 V$ ?2 F public class Test
5 a) S* r: H6 J; h public static void main throws Exception& A/ L0 V$ p- ?. P: G0 l1 A( m0 H) p; ~
} a9 P8 B! x t8 C
感谢感动paulex师长教师的辅佐,在paulex师长教师的提醒下,我根基上年夜白了上述问题的原因。下面是paulex师长教师的解答:
- s t* ]; i! f* K+ X3 g 因为Generic, 编译器可以在编译期获得类型所以可以编译这类代码。你将下面那两行改成
6 B( ~/ }& V7 I0 D Class? extends Test c = obj).getClass;
% | {! G. [- {" d% S/ Y c.newInstance.func;
$ g! h; ~* g# `3 y, _ 应该就能经由过程编译了。" j' f. E# G1 Z7 @7 l6 `$ w
下面是我在paulex师长教师解答的基本上,对问题的进一步诠释:
4 U7 P! ?, m/ B3 {# v 在JDK 1.5中惹人范型后,Object.getClass体例的界说如下: ^" @; r0 Z8 `. X! L
public final Class? extends Object getClass
. w$ m5 k9 ^2 e; H u# ` Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.: r# N" [9 K5 B$ p2 _
Returns:. w1 P( u- X7 `& ?0 h& K% f2 D
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.
- P, p/ X0 v* q" z7 K 这声名obj).getClass语句返回的对象类型为Class? extends Test,而ClassT的newInstance体例的界说如下:
N3 M9 g3 E: v public T newInstance throws InstantiationException,IllegalACCESSException$ P) f: N" d( [5 y1 b4 K
即对于编译器看来,ClassTest的newInstance体例的对象类型为Test,而obj).getClass返回的为对象类型为Class? extends Test,所以,编译器认为obj).getClass.newInstance返回的对象类型为Test。5 a. k8 b( S, w/ \- B% {
下面这两行代码之所以无法经由过程编译
2 C) { U! ]& n Class c = obj).getClass;% Q2 y0 ?. ^/ D& Q! U
c.newInstance.func;
: ^: Q# V$ N V ]. I4 l, C 是因为obj).getClass返回的为对象类型为Class? extends Test,可是我们在第一行将结不美观强制转换成了Class,然后再去挪用Class的newInstance体例,而不是去挪用ClassTest的newInstance体例,编译器当然不再认为Class的newInstance体例返回的对象为Test了。 |