JAVA核心:反射

1. Class 类与 Java 反射

在这里插入图片描述

1.1 访问构造方法

在通过下列一组方法访问构造方法时,将返回Constructor类型的对象或数组。每个Constructor对象代表一个构造方法,利用Constructor对象可以操纵相应的构造方法。

  • getConstructors()
  • getConstructor(Class<?>… parameterTypes)
  • getDeclaredConstructors()
  • getDeclaredConstructor(Class<?>… parameterTypes)

如果是访问指定的构造方法,需要根据该构造方法的入口参数的类型来访问。
在这里插入图片描述
在这里插入图片描述
先创建Example类:

public class Example {
   
	public int id;
	public String name;
	private double price;
	
	// 点击菜单栏中Source->Generate Constructor using Fields创建构造方法
	public Example() {
   
		super();
	}

	public Example(int id) {
   
		super();
		this.id = id;
	}

	private Example(int id, String name, double price) {
   
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}

	// 点击菜单栏中Source->Generate to String
	@Override
	public String toString() {
   
		return "Example [id=" + id + ", name=" + name + ", price=" + price + "]";
	}
}

然后创建Demo类:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;

public class Demo {
   
	public static void main(String[] args) {
   
		try {
   
			Class c = Class.forName("mr.Example");
			Constructor cons[] = c.getDeclaredConstructors();// 获取所有构造方法
			for (Constructor con : cons) {
   
				System.out.print(Modifier.toString(con.getModifiers()) + " ");// 修饰符
				System.out.print(con.getName() + "(");// 方法名
				Class paras[] = con.getParameterTypes();// 方法的参数
				for (int i = 0; i < paras.length; i++) {
   
					System.out.print(paras[i].getSimpleName() + " args ");
					if (i < paras.length - 1) {
   // 如果不是最后一个,加个逗号
						System.out.print(",");
					}
				}
				System.out.println(") { }");
			}

			Constructor cs1 = c.getDeclaredConstructor();// 无参数构造方法
			Object obj = cs1.newInstance();
			System.out.println(obj.toString());

			Constructor cs2 = c.getDeclaredConstructor(int.class);// 有参数构造方法
			obj = cs2.newInstance(648);
			System.out.println(obj.toString());

			Constructor cs3 = c.getDeclaredConstructor(int.class, String.class, double.class);// 无参数构造方法
			cs3.setAccessible(true);// 获取私有方法操作权限
			obj = cs3.newInstance(1, "diamond", 648);
			System.out.println(obj.toString());

		} catch (ClassNotFoundException e) {
   
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
   
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
   
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
   
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
   
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
   
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException</
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值