Class類(java.lang.Class) public final class Classextends Objectimplements Serializable, GenericDeclaration, Type, AnnotatedElement Class對象的三種實例化模式 對象.g... ...
Class類(java.lang.Class)
public final class Class<T>extends Objectimplements Serializable, GenericDeclaration, Type, AnnotatedElement
Class對象的三種實例化模式
- 對象.getClass();
- 類.class;
- Class.forName(String className)
public static Class<?> forName(String className) throws ClassNotFoundException
Class類對象(類)的實例化形式
Class類的對象.newInstance();
public T newInstance()throws InstantiationException, IllegalAccessException
反射的一般實例:
class Phone {
public Phone() {
System.out.println("Phone類的構造方法");
}
public String toString() {
return "是一部手機";
}
}
public class Test {
public static void main(String[] args) throws Exception {
Class<Phone> cls = Phone.class;
Phone p2 = cls.newInstance();
System.out.println("p2: " + p2);
}
}