反射机制——获取Class中的方法

一、代码说明

    1、获取指定class文件中的公共方法

/**
	 * @throws Exception *
	* @Title: getMethodDemo 
	* @Description: 获取指定class文件中的公共方法
	* @param 
	* @return void
	* @throws 
	*/
	private static void getMethodDemo() throws Exception {
		String name = "com.lh.reflection.bean.Person";

		// 寻找该类名称字节码文件 ,并加载至内存,生成Class对象
		Class<Person> clazz = (Class<Person>) Class.forName(name);
		
		Method[] methods = clazz.getMethods();//获取的都是公有的方法
		methods = clazz.getDeclaredMethods();//获取的是本类的所有方法,包含私有方法
		
		for(Method mehtod: methods){
			System.out.println(mehtod);
		}
		
	}

    2、反射调用无参方法

/**
	 * @throws Exception *
	* @Title: getMethodDemo2 
	* @Description: 反射调用无参方法
	* @param 
	* @return void
	* @throws 
	*/
	private static void getMethodDemo2() throws Exception {
		String name = "com.lh.reflection.bean.Person";

		// 寻找该类名称字节码文件 ,并加载至内存,生成Class对象
		Class<Person> clazz = (Class<Person>) Class.forName(name);
		
//		Method method = clazz.getMethod("show",null);
		Method method = clazz.getMethod("show");
		
//		Person person = clazz.newInstance();
		Constructor<Person> constructor = clazz.getConstructor(String.class,int.class);
		Person person = constructor.newInstance("小明",20);
		
//		method.invoke(person,null);
		method.invoke(person);
	}

    3、反射调用有参方法

/**
	 * @throws Exception *
	* @Title: getMethodDemo3 
	* @Description: 反射调用有参方法
	* @param 
	* @return void
	* @throws 
	*/
	private static void getMethodDemo3() throws Exception {
		String name = "com.lh.reflection.bean.Person";

		// 寻找该类名称字节码文件 ,并加载至内存,生成Class对象
		Class<Person> clazz = (Class<Person>) Class.forName(name);
		
		Method method = clazz.getMethod("paramMethod", String.class,int.class);
		
		Person person = clazz.newInstance();
		method.invoke(person, "小明",20);
	}

    4、反射调用静态无参方法

/**
	 * @throws Exception *
	* @Title: getMethodDemo4 
	* @Description: 反射调用静态无参方法
	* @param 
	* @return void
	* @throws 
	*/
	private static void getMethodDemo4() throws Exception {
		String name = "com.lh.reflection.bean.Person";

		// 寻找该类名称字节码文件 ,并加载至内存,生成Class对象
		Class<Person> clazz = (Class<Person>) Class.forName(name);
		
		Method method = clazz.getMethod("staticMethod");
		
		Person person = clazz.newInstance();
		
		method.invoke(person);
	}

    5、Person类

/** 
* @Title: Person.java 
* @Package com.lh.reflection.bean 
* @Description: TODO
* @author Liu 
* @date 2018年1月23日 下午3:06:10 
* @version V1.0 
*/
package com.lh.reflection.bean;

/**
 * @ClassName: Person
 * @Description: Person实体类
 * @author Liu
 * @date 2018年1月23日 下午3:06:10
 * 
 */
public class Person {
	private int age;
	private String name;

	public Person() {
		System.out.println("constructor no param run...");
	}

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

		System.out.println("constructor with param run..." + this.name + ":" + this.age);
	}

	public void show() {
		System.out.println(name + "...show run..." + age);
	}

	private void privateMethod() {
		System.out.println("method run");
	}

	public void paramMethod(String str, int num) {
		System.out.println("paramMethod run..." + str + ":" + num);
	}

	public static void staticMethod() {
		System.out.println("static method run...");
	}

}

 

转载于:https://my.oschina.net/Howard2016/blog/1612192

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值