- package text1;
- import java.lang.reflect.Method;
- public class A{
-
- public static void main(String[] args) throws Exception {
- Class class1 = Class.forName("text1.A");
- Object newInstance2 = class1.newInstance();//不用泛型实例对象的类型是Object
- Method method = class1.getMethod("run", null);
- method.invoke(new A(), null);
- Class<A> name = (Class<A>) Class.forName("text1.A");
- A newInstance3 = name.newInstance();//用了泛型代码就自动生成了。
- B newInstance = name.newInstance();//这里报错了,这就是为什么Class类要用泛型。用于不用区别也在这;
-
- }
- public void run(){
- System.out.println("run");
- }
- }
- class B{
-
- }
复制代码 |