import java.lang.reflect.*;
import java.awt.*;
class Sample
{
public static void main(STring args[])
{
Rectangle r = new Rectangle();
show(r);
}
static void show(Object o)
{
Class c = o.getClass();
Constructor [] the = c.getConstructors();
for(int i=0;i<the.length;i++)
{
System.out.println("(");
Class[] param = the[i].getParameterTypes();
for(int k=0;k<param.length;k++)
{
String parame = param[k].getName();
System.out.print(parame+" ");
}
System.out.println(")");
}
}
}
本文通过一个Java示例程序展示了如何使用反射API来获取并打印出对象所属类的所有公共构造方法及其参数类型。这对于理解Java反射机制及其实现细节非常有用。
445

被折叠的 条评论
为什么被折叠?



