首页 > 学院 > 开发设计 > 正文

构造方法类的反射机制

2019-11-11 05:25:10
字体:
来源:转载
供稿:网友
/** * 类的反射 * 成员属性是Field类的对象 * 构造方法是Constructor类的对象 * @author Administrator * */public class MethodName { public String m; public int i; public char ch; public double d; public MethodName(String m, int i, char ch, double d) { super(); this.m = m; this.i = i; this.ch = ch; this.d = d; } public static void main(String[] args) throws ClassNotFoundException { Chengyuan(); }/** * 获得反射类的所有构造方法 * @throws ClassNotFoundException */ @SupPRessWarnings("rawtypes") private static void Chengyuan() throws ClassNotFoundException { Class c=Class.forName("lei.MethodName");//动态加载 Constructor[] con = c.getConstructors(); for (Constructor cons: con) { System.out.print(cons.getName()+"("); Class[] cl=cons.getParameterTypes();//获得参数列表类型 for (Class cl1 : cl) { System.out.print(cl1.getSimpleName()+","); } System.out.println(")"); }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表