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());
}
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);
}
}
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(“)”);
}
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(“)”);
}
相关文章推荐
- JAVA反射补充
- java发送post请求代码
- Java ClassLoader基础及加载不同依赖 Jar 中的公共类
- Java Servlet
- 深入理解Java Annotation
- Java 回顾笔记_异常
- java有参构造方法和无参构造方法
- Java NIO使用及原理分析 (三)
- Spring MVC + MyBatis整合(IntelliJ IDEA环境下)
- java多线程
- 算法导论--JAVA实现合并排序详解
- Java NIO使用及原理分析(二)
- leetcode:Merge Sorted Array 【Java】
- axis2通过wsdl2java.bat生成webservice异步客户端程序
- Iframe 高度自适应解决方案
- Java中gitter、setter方法和public的区别
- Java - Hash - HashMap与HashTable
- Java抽象类
- Java NIO使用及原理分析 (一)
- Java基础知识点记录(主要记录一些不常见或者容易忘记的知识点,时刻更新)