反射之方法

所用实体类:

package reflect;

//定义一个学生类,作为测试项
public class Student {
	private String id;
	public int age;
	String phone;
	
	//无参构造方法
	public Student(){}
	
	//全参构造方法
	public Student(String id, int age, String phone) {
		super();
		this.id = id;
		this.age = age;
		this.phone = phone;
	}
	
	//部分参数构造方法
	public Student(String id, int age) {
		super();
		this.id = id;
		this.age = age;
	}
	
	//私有的构造方法
	private Student(String id) {
		super();
		this.id = id;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	//定义一个Public方法
	public void showPublic(){
		System.out.println("我是公有的方法");
	}
	
	//定义一个私有的方法
	private void showPrivate(){
		System.out.println("我是私有的方法");
	}
	
	//定义一个受保护的方法
	protected void showProtected(){
		System.out.println("我是受保护的方法");
	}
	
	//定义一个默认的方法
	void show(){
		System.out.println("我是默认的方法");
	}
	
	//定义一个有返回值的方法
	public String showReturn(){
		return "我是有返回值的方法";
	}

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

反射操作:

package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;

public class TestMothod {
	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		//创建运行时类对象
		String destPath="day31reflect.Student";
		Class<?> student = Class.forName(destPath);    //创建了一个student运行时类对象
		
		//如何获取所有方法的信息?
		Method m = student.getMethod("showPublic");   //找到,public
		System.out.println(m);
		
	/*	Method m1 = student.getMethod("showPrivate");   //未找到,private,报异常
		System.out.println(m1);*/
		
		System.out.println("---------------1--------------------");
		
		//如何获取所有方法,不管是不是共有的,包括set和get方法,仅获取的是本类中的方法
		Method[] method = student.getDeclaredMethods();
		for (Method method2 : method) {
			System.out.println("方法信息:"+method2);
		}
		System.out.println("--------------2-----------------------");
		
		//获取本类及其父类,实现的接口中的所有public方法
		Method[] method1 = student.getMethods();
		for (Method method2 : method1) {
			System.out.println(method2);
		}
		System.out.println("--------------3--------------------------");
		
		//分别获取方法的修饰符、返回值、名字、参数类型
		for (Method method2 : method) {
			//获取修饰符
			System.out.println("修饰符为:"+method2.getModifiers()+"    "+Modifier.toString(method2.getModifiers()));
			//返回值类型为:
			System.out.println("返回值为:"+method2.getReturnType());
			//方法名:
			System.out.println("方法名为:"+method2.getName());
			//方法中的参数的修饰符
			Type[] type = method2.getGenericParameterTypes();
			System.out.print("形参修饰符为:"+" ");
			for (Type type2 : type) {
				System.out.println(type2+" ");
			}
			System.out.println();
			System.out.println("~~~~~~");
		}
		System.out.println("------------4----------------");
		
		//调用无参普通方法,需要对象调用,所以需要先创建一个对象
		Student s = (Student) student.newInstance();  //需要强转的原因:student对象时Class<?>类型的,如果是Class<Student> student = student.class;就不需要强转
		//找到对象中的方法
		Method m1= student.getDeclaredMethod("showReturn");
		Object obj = m1.invoke(s);
		System.out.println("返回值:"+obj);
		System.out.println("-------------5---------------------");
		
		//调用有参方法
		Method m2 = student.getDeclaredMethod("setAge", int.class);
		m2.invoke(s, 21);   //对指定方法进行赋值
		System.out.println(s.getAge());    //验证
		System.out.println("----------------6---------------------------");
		
		//调用私有方法
		Method m3 = student.getDeclaredMethod("showPrivate");
		m3.setAccessible(true);   //由于是私有的,所以要进行权限的释放
		m3.invoke(s);
		System.out.println("-----------------7----------------------------");
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值