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()搞不清除了求指教
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.