//1.导入反射用到的包
import java.lang.reflect.*;
class ClassTest
{
public static void main(String[] args)
{
try
{
//2.根据参数载入类实例
Class c = Class.forName(args[0]);
//3.根据载入构造函数
Constructor[] cons = c.getDeclaredConstructors();
//获得构造参数的类型
Class[] paraTypes = cons[0].getParameterTypes();
Object[] paraValues = new Object[paraTypes.length];
for(int i = 0; i < paraValues.length; i ++)
{
if(paraTypes[i].isPrimitive())
paraValues[i] = new Integer(i+3);
}
Object o = cons[0].newInstance(paraValues);
Method[] ms = c.getDeclaredMethods();
ms[0].invoke(o,null);
}
catch(Exception ex)
{}
}
}
class Point
{
int x;
Point(int x)
{
this.x = x;
}
public void outPut()
{
System.out.println("x ="+x);
}
}
import java.lang.reflect.*;
class ClassTest
{
public static void main(String[] args)
{
try
{
//2.根据参数载入类实例
Class c = Class.forName(args[0]);
//3.根据载入构造函数
Constructor[] cons = c.getDeclaredConstructors();
//获得构造参数的类型
Class[] paraTypes = cons[0].getParameterTypes();
Object[] paraValues = new Object[paraTypes.length];
for(int i = 0; i < paraValues.length; i ++)
{
if(paraTypes[i].isPrimitive())
paraValues[i] = new Integer(i+3);
}
Object o = cons[0].newInstance(paraValues);
Method[] ms = c.getDeclaredMethods();
ms[0].invoke(o,null);
}
catch(Exception ex)
{}
}
}
class Point
{
int x;
Point(int x)
{
this.x = x;
}
public void outPut()
{
System.out.println("x ="+x);
}
}