JavaSE 反射——获取及反编译Method

一、获取Method

  • 继承的方法Method无法反射,会被编译器强制执行,因此反射一个类的方法时不考虑父类的方法,只考虑当前类的方法

1. 常用方法

  • 参考:Method 类常用方法
  • Class.getMethod(String name,Class…parameterTypes);
    根据方法名称和相关参数,来定位需要查找的Method对象并返回,parameterTypes:Method的参数类型的列表(参数顺序需按声明Method时的参数列表排列)
  • Class.getMethods();
    获取所有public方法,包括类自身声明的public方法、父类中的public方法、实现的接口方法
  • Class.getDeclaredMethod(String name, Class… parameterTypes);
    返回一个Method对象,反映此Class对象所表示的类或接口的指定已声明方法
  • Class.getDeclaredMethods();
    获取本类中所有方法,包括类自身的方法、重写的父类的方法、实现的接口方法

2. 测试用例

public class User {
   

	public int id;
	
	private String name;
	
	String address;
	
	protected int sno;
	
	public User() {
   }
	
	public User(int id, String name, String address, int sno) {
   
		super();
		this.id = id;
		this.name = name;
		this.address = address;
		this.sno = sno;
	}
	
	public void study() {
   
		System.out.println("study...");
	}
	
	public void eat(int a,String b) {
   
		System.out.println("eat...");
	}

	@Override
	public String toString() {
   
		return "User [id=" + id + ", name=" + name + ", address=" + address + ", sno=" + sno + "]";
	}

	@Override
	public int 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jayco-J

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值