反射之Method类



这是无参方法的调用

package com.itheima;

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

/**   
 * 无参方法调用 	 
 */
public class Test5 {
	public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		Reflect_Class ref=new Reflect_Class();//实例化类
		Method method=ref.getClass().getMethod("method");//通过反射获取Method对象
		method.invoke(ref);//通过Method类中的invoke()方法调用Reflect_Class类中的方法
	}
}
//定义一个类
class Reflect_Class{
	//在类中定义一个方法
	public void method(){
		System.out.println("aaaaaaaaaaaaaa");//打印字符串
	}
}

这是有参数方法的调用

package com.lianxi;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class LianXi{
	public static void main(String[] args) throws Exception {
//		method();
//		method1();
		method_main();
	}
	/*
	 * 通过反射调用另一个类中的主方法
	 */
	public static void method_main()throws Exception{
		String mainPath="com.lianxi.Main_Class";
		Method m=Class.forName(mainPath).getMethod("main",String[].class);
		//这里要注意,因为JDK1.5才出现的可变参数,之前用的都是数组,为了兼容以前的,传入数组会被分开了解析,所以这里这样直接写,就会被解析成三个字符串对象,而不是一个字符串数组了
//		m.invoke(null,new String[]{"111","222","333"});//注意因为类中的主方法值静态的,所以invoke的第一个参数要用null
		//解决方式一:把要传入的数组再封装一层,变成一个数组就可以了
//		m.invoke(null,new Object[]{new String[]{"111","222","333"}});
		//解决方式二:直接把字符串数组进行强转
		m.invoke(null,(Object)new String[]{"111","222","333"});
	}
	public static void method1() throws Exception{
		String str="abcdefg";
		Method m=String.class.getMethod("charAt",int.class);
		System.out.println(m.invoke(str,2));//如果invoke()方法第一个参数为null这意味着该Method对象对应的是一个静态方法,因为静态方法调用不需要实例
		System.out.println("-----------------------------");
		LianXi_reflect ref=new LianXi_reflect();
		Method mm=ref.getClass().getMethod("demo");
		mm.invoke(ref);//执行反射指定方法
	}
}
class Main_Class{//定义一个类
	public static void main(String[]args){//main方法打印传进来的参数
		for(String arg:args){
			System.out.println(arg);
		}
	}
}
class LianXi_reflect{
	public void demo(){
		System.out.println("cccccccccccc");
	}
}




反射Method的使用可以通过以下步骤: 1. 获取Class对象:首先需要获取要反射的Class对象,可以通过名调用Class.forName()方法或者使用的class属性获取。 2. 获取Method对象:接下来需要获取要反射的方法的Method对象,可以通过Class对象的getMethod()、getDeclaredMethod()、getMethods()、getDeclaredMethods()等方法获取。 3. 设置方法可访问性:如果要反射的方法是私有的,需要设置方法的可访问性为true,否则会抛出IllegalAccessException异常。 4. 调用方法:最后,使用Method对象的invoke()方法调用方法,并传入方法所属的对象和方法参数,如果方法没有返回值,invoke()方法返回null,否则返回方法返回值。 以下是一个示例代码,演示了如何使用反射Method来调用一个的方法: ```java public class MyClass { private void myMethod(String str, int num) { System.out.println("My method: " + str + ", " + num); } } public class Main { public static void main(String[] args) throws Exception { MyClass obj = new MyClass(); Class<?> cls = obj.getClass(); Method method = cls.getDeclaredMethod("myMethod", String.class, int.class); method.setAccessible(true); method.invoke(obj, "Hello", 123); } } ``` 在上述示例代码中,我们首先创建了一个MyClass,并定义了一个私有的myMethod()方法。然后我们通过Class的getDeclaredMethod()方法获取了myMethod()方法的Method对象,并设置其可访问性为true。最后,我们使用Method对象的invoke()方法调用了myMethod()方法,并传入了一个字符串和一个整数作为参数。运行上述代码,输出结果为: ``` My method: Hello, 123 ``` 这说明我们成功地使用反射Method调用了一个的私有方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

King·Forward

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

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

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

打赏作者

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

抵扣说明:

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

余额充值