java-reflejo-2

package reflect_2020_09_29;
import java.lang.reflect.*;


/**
 * 学习通过反射访问 构造方法 的方法
 * 
 * 1.要使用java.lang.reflect.Constructor这个包
 * 
 * 2.Constructor是对类进行操作,第一步首先要申明这个类
 * 
 *		1.获取类的对象
 *			1.Class c=Class.forName("类名");
 *			2.Class c=类名.class;
 *			3.Class c=对象名.getClass();
 *		
 *		2.由于getConstructors()是返回的是对象或者是Constructor的对象或者数组
 *		
 *			Constructor [] con =c.getConstructors();获取类的构造方法,这是公共的,包括继承的超类构造方法
 *
 * 3.常用的Constructor的方法
 * 			
 * 			1.get.Modfiers();解析构造方法使用的修饰符
 * 			2.getName();构造方法的名字
 * 			3.getParameterTypes();以class数组的方式获取该构造方法的各个类型
 * 
 * 4.获取类的构造方法
 * 
 * 			1.获取所有公有(public)构造方法(包括超类):
 * 		
 * 				Constructor [] con =c.getConstructors();
 * 
 * 			2.获取指定的公有的(public)构造方法(包括超类): 不可用数组,因为返回来就一个
 * 
 *  			Constructor con =c.getConstructor(Class<?>···parameterTypes);
 *  
 * 			3.获取本类的所有构造方法(包括私有的):
 * 
 * 				Constructor [] con=c.getDeclaredConstructors();
 * 
 * 			4.获取本类的指定的构造方法(包括私有的):
 * 
 * 				Constructor  con=c.getDeclaredConstructor(Class<?>···parameterTypes);
 * 
 * @author 小虎牙
 *
 */
public class reflect_2 {

	public static void main(String[] args) {

		// TODO 自动生成的方法存根
		try {

			// 第一步,获取反射的类
			//Class c = Class.forName("reflect_2020_09_29.pension");// 方法一:这里要注意是包名.类名
			Class c =pension.class;//方法二;
			//pension pen=new pension();//方法三
			//Class c=pen.getClass();
			// 2.查找反射类的所有的公有构造方法

			System.out.println("--------所有的公有构造方法----------");
			Constructor[] con = c.getConstructors();

			// 遍历
			for (Constructor constructor : con) {
				System.out.println(constructor);// 这里就是输出所有的构造方法(public)
				System.out.println("-----------------------");
				// System.out.println(Modifier.toString(constructor.getModifiers()));
			}

			for (Constructor constructor : con) {
				System.out.print(Modifier.toString(constructor.getModifiers()) + " ");
				System.out.print(constructor.getName() + "(");
				Class[] arge = constructor.getParameterTypes();// 把构造方法的参数放在arge数组里面
				for (int i = 0; i < arge.length; i++) {
					System.out.print(arge[i].getSimpleName());// 这里建议用getSimpleName
					if (i != arge.length - 1) {// 在参数中间加逗号
						System.out.print(",");
					}
				}
				System.out.print("){}" + "\n" + "\n");
			}

			// 3.查找反射类的指定的公有构造方法,注意这里,特定的只能是一个,不能用数组来接收
			System.out.println("--------指定的公有构造方法----------");

			Constructor<pension> con1 = c.getConstructor(int.class);
			System.out.print(Modifier.toString(con1.getModifiers()) + " " + con1.getName() + "(");
			Class[] arge = con1.getParameterTypes();
			for (int i = 0; i < arge.length; i++) {
				System.out.print(arge[i].getSimpleName());
				if (i != arge.length - 1) {
					System.out.print(",");
				}
			}
			System.out.println("){}");

			// 4.查找反射类的所有的构造方法(只在本类)

			System.out.println("--------所有的构造方法遍历----------");
			con = c.getDeclaredConstructors();
			// 遍历
			for (Constructor constructor : con) {
				System.out.println(constructor);// 这里就是输出所有的构造方法(public)
				System.out.println("-----------------------");
				// System.out.println(Modifier.toString(constructor.getModifiers()));
			}
			System.out.println("--------所有的构造方法----------");
			for (Constructor constructor : con) {
				System.out.print(Modifier.toString(constructor.getModifiers()) + " ");
				System.out.print(constructor.getName() + "(");
				arge = constructor.getParameterTypes();// 把构造方法的参数放在arge数组里面
				for (int i = 0; i < arge.length; i++) {
					System.out.print(arge[i].getSimpleName());// 这里建议用getSimpleName
					if (i != arge.length - 1) {// 在参数中间加逗号
						System.out.print(",");
					}
				}
				System.out.print("){}" + "\n" + "\n");
			}

			// 5.查找反射类的指定的的构造方法(只在本类)
			System.out.println("--------指定的构造方法----------");
			con1 = c.getDeclaredConstructor(int.class, int.class, String.class);
			System.out.print(Modifier.toString(con1.getModifiers()) + " " + con1.getName() + "(");
			arge = con1.getParameterTypes();
			for (int i = 0; i < arge.length; i++) {
				System.out.print(arge[i].getSimpleName());
				if (i != arge.length - 1) {
					System.out.print(",");
				}
			}
			System.out.println("){}");

			// 6.利用构造方法,建立新的类

			System.out.println("------------构造无参函数的类-----------");
			con1 = c.getDeclaredConstructor(null);
			pension p = con1.newInstance(null);
			System.out.println(p);
			System.out.println("------------构造有参函数的类-----------");
			con1 = c.getDeclaredConstructor(int.class);
			p = con1.newInstance(18);
			System.out.println(p);
			System.out.println("------------构造私有,有参函数的类-----------");
			con1 = c.getDeclaredConstructor(int.class, int.class, String.class);
			// 因为这里是私有的,要处理一下
			con1.setAccessible(true);// 设置操作权限
			p = con1.newInstance(01, 20, "小明");
			System.out.println(p);

		} catch (NoSuchMethodException | SecurityException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}

}

A continuación, miramos el contenido de la clase a reflejar.

package reflect_2020_09_29;

public class pension {
	private int id;
	private int age;
	private String name;

	public pension() {
		// TODO 自动生成的构造函数存根
	}

	public pension(int id) {
		super();
		this.id = id;

	}

	public pension(int age, String name) {
		super();
		this.age = age;
		this.name = name;

	}

	private pension(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}

	@Override
	public String toString() {
		return "pension [id=" + id + ", age=" + age + ", name=" + name + "]";
	}

}

Supongo que te gusta

Origin blog.csdn.net/huiguo_/article/details/108868690
Recomendado
Clasificación