您的位置:首页 > 编程语言 > Java开发

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

2016-03-03 15:11 549 查看
我们最常用的获取反射的方案:

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(“)”);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: