package reflection;import java.lang.reflect.Constructor;public class Test {
public static void main(String[] args) {
Class<String> cl = String.class;//正确
Class<String> cl1 = new String().getClass(); //错误
Class<? extends String> cl2 = new String().getClass();//只能这样
}}
String.class和new String().getClass()搞不清除了求指教

解决方案 »

  1.   

    Class<String> cl1 = new String().getClass(); //错误因为你new一个对象,在编译时期无法确定你new的是什么对象,只能说你new的对象必须是String或者String的子类,虽然String类final的,其他类同理。所以 Class<? extends String> cl2 = new String().getClass();//只能这样
      

  2.   

    楼上兄弟说的是对的,因为java有动态绑定这样一个特性
      

  3.   

    可以简单点理解为Java的编译器还不够聪明,编译期间无法智能识别出下面两者的差异:Class<? extends Object> cl2 = new Object() {private int a = 0;}.getClass();Class<? extends Object> cl2 = new Object().getClass();所以只好一视同仁的按照前者处理了。
      

  4.   

    Object中定义的getClass()方法的返回值类型是Class<?>。。任何子类中也没重载,这Class<? extends String>貌似是编译器附加了一个要求API看的也是云里雾里:public final Class<?> getClass()
    Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.
    The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment:Number n = 0; 
    Class<? extends Number> c = n.getClass();Returns:
    The Class object that represents the runtime class of this object.
    See Also:
    Literals, section 15.8.2 of The Java™ Language Specification.
      

  5.   

    getClass()还是final的。。这不太像是动态绑定吧。。