package cn.day1;
class Sub1 implements Parent
{
public void method1(){
System.out.println("sub1");
}
}
class Sub2 implements Parent
{
public void method1(){
System.out.println("sub2");
}
}
public class MyReflect
{
public static Object generator(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
return Class.forName( className ).newInstance();
}
public static void main( String[] args ) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
Parent o = (Parent) generator( args[0] );
o.method1();
}
}
反射练习代码
最新推荐文章于 2024-09-01 16:35:45 发布