会计考友 发表于 2012-8-4 12:44:44

JAVA认证:JDK1.5编译中的一个罕见问题

学员冯伟立今天午时问了我一个有趣的Java编译问题,我也无法给他诠释,不知道有没有路过的高人能够诠释清嚣张原因,望不惜赐教!
  下面轨范的main体例中的第二行代码和注释中的两行代码表达的意思完全不异,注释中的两行代码不能经由过程编译(这很等闲理解),而第二行(采用体例挪用链)却可以顺遂经由过程编译(这就很难理解了)。
  public class Test
  public static void main throws Exception
  }
  感谢感动paulex师长教师的辅佐,在paulex师长教师的提醒下,我根基上年夜白了上述问题的原因。下面是paulex师长教师的解答:
  因为Generic, 编译器可以在编译期获得类型所以可以编译这类代码。你将下面那两行改成
  Class? extends Test c = obj).getClass;
  c.newInstance.func;
  应该就能经由过程编译了。
  下面是我在paulex师长教师解答的基本上,对问题的进一步诠释:
  在JDK 1.5中惹人范型后,Object.getClass体例的界说如下:
  public final Class? extends Object getClass
  Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
  Returns:
  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.
  这声名obj).getClass语句返回的对象类型为Class? extends Test,而ClassT的newInstance体例的界说如下:
  public T newInstance throws InstantiationException,IllegalACCESSException
  即对于编译器看来,ClassTest的newInstance体例的对象类型为Test,而obj).getClass返回的为对象类型为Class? extends Test,所以,编译器认为obj).getClass.newInstance返回的对象类型为Test。
  下面这两行代码之所以无法经由过程编译
  Class c = obj).getClass;
  c.newInstance.func;
  是因为obj).getClass返回的为对象类型为Class? extends Test,可是我们在第一行将结不美观强制转换成了Class,然后再去挪用Class的newInstance体例,而不是去挪用ClassTest的newInstance体例,编译器当然不再认为Class的newInstance体例返回的对象为Test了。
页: [1]
查看完整版本: JAVA认证:JDK1.5编译中的一个罕见问题