JAVA 采用反射机制 获取构造方法

我们最常用的获取反射的方案:
Person.java(需要反射的类)
package cn.sunmeng.reflection.construction;
import java.util.List;
public class Person {

/**
* 无参构造函数
*/
public Person(){
System.out.println(“person”);
}
/**
* 一个参数构造函数
* @param name
*/
public Person(String name){
System.out.println(“name: “+name);
}
/**
* 两个参数构造函数
* @param name
* @param num
*/
public Person(String name,int num){
System.out.println(“name: “+name+” num: “+num);
}
/**
* 私有构造函数
* @param list
*/
private Person(List list){
System.out.println(“list size: “+list.size());
}

}

TestConstru.java(具体的反射方法)
package cn.sunmeng.reflection.construction;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
public class TestConstru {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//getPerson1();
//getPerson2();
//getPerson3();
getPerson4();
}

/**
* 采用反射机制 获取Person类中函数:public Person()
* @throws Exception
*/
public static void getPerson1() throws Exception{
//1.加载类路径
Class clazz=Class.forName(“cn.sunmeng.reflection.construction.Person”);
//2.获取无参数构造函数
Constructor c=clazz.getConstructor(); //这里可以传入null 代表无参数构造方法
//3.实例化反射到的对象
Person person=(Person)c.newInstance(); //这里可以传入null 代表无参数构造的值

//直接使用clazz实例化对象无参数构造方法,等同于以上2,3两句
//Person person=(Person)clazz.newInstance();
System.out.println(person);
}
/**
* 采用反射机制 获取Person类中函数:public Person(String name)
* @throws Exception
*/
public static void getPerson2() throws Exception{
//1.加载类路径
Class clazz=Class.forName(“cn.sunmeng.reflection.construction.Person”);
//2.获取有一个参数并且为String的类型参数的方法
Constructor c=clazz.getConstructor(String.class);
//3.实例化对象传入参数值
Person p=(Person)c.newInstance(“张三”);

System.out.println(p);
}
/**
* 采用反射机制 获取Person类中函数:public Person(String name,int num)
* @throws Exception
*/
public static void getPerson3() throws Exception{
//1.加载类路径
Class clazz=Class.forName(“cn.sunmeng.reflection.construction.Person”);
//2.获取有两个参数并且第一个参数为String,第二个参数为int类型的方法
Constructor c=clazz.getConstructor(String.class,int.class);
//3.实例化对象并传入值
Person p=(Person)c.newInstance(“李四”,20);

System.out.println(p);
}
/**
* 采用反射机制 获取Person类中函数:private Person(List list)
* @throws Exception
*/
public static void getPerson4() throws Exception{
//1.加载类路径
Class clazz=Class.forName(“cn.sunmeng.reflection.construction.Person”);
//2.获取有一个参数并且参数为List的构造方法,改构造方法未私有,必需使用DeclaredConstructor方法获取
Constructor c=clazz.getDeclaredConstructor(List.class);
//3.暴力访问私有构造方法
c.setAccessible(true);
//4.为构造方法传入参数
Person p=(Person)c.newInstance(new ArrayList());
System.out.println(p);
}
}


在反射的机制中,可以便利出构造方法:


Class clazz = Class.forName(“cn.sunmeng.reflection.construction.Person”);
Constructor[] cons = clazz.getDeclaredConstructors();//获取所有构造方法
for (int i = 0; i < cons.length; i++) {
Constructor con = cons[i];// 取出第i个构造方法
System.out.print(Modifier.toString(con.getModifiers())); //打印该构造方法的前缀修饰符
System.out.println( con.getName() + “(“);//打印该构造方法的名字
// 打印该构造方法的参数
Class[] parameterTypes = con.getParameterTypes();
for(int j = 0; j < parameterTypes.length; j++) {
System.out.print(parameterTypes[j].getName());
if((j + 1) != parameterTypes.length) {
System.out.print(“, “);
}
}
System.out.print(“)”);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值