反射(Method类)

Method类代表某个类中的一个成员方法。
反射通过它可以调用某个类中的一个方法。

package cn.itcast.reflect1;
public class Teacher {
    public int age=10;
    private String name="Tom";
    public void show()
    {
        System.out.println("name:"+name+" age:"+age);
    }
    public void show(String msg)
    {
        System.out.println(msg);
    }
    public String show(String msg,int i)
    {
        return msg+" "+i;
    }
}
package cn.itcast.reflect1;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
public class Demo1 {
    @Test
    //得到Teacher类中的show方法的Method对象
    public void fun1() throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        //1、得到Class
        Class c=Teacher.class;
        //2、得到Method
        Method m1=c.getDeclaredMethod("show");
        Method m2=c.getDeclaredMethod("show", String.class);
        //3、让方法执行
        Teacher t=(Teacher)c.newInstance();//得到Teacher的对象
        m1.invoke(t);//相当于t.show();
        m2.invoke(t, "good");//相当于t.show("good")
    }
    @Test
    public void fun2() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException
    {
        Class c=Teacher.class;
        Method m=c.getMethod("show", String.class,int.class);
        Object returnValue=m.invoke(c.newInstance(), "Tom",20);//相当于t.show("Tom",20)
        //returnValue相当于t.show("Tom",20)的返回值
        System.out.println(returnValue);
    }
}

1、对于static方法怎么调用呢?
因为static方法是静态方法不需要对象,所以invoke函数的第一个参数写为null。
2、方法的参数如果是数组怎么调用?
在调用invoke时,第二个参数强制转换成Object,或者在外面在包装一层数组。

package cn.itcast.reflect1;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
public class Demo3 {
    public static void main(String[] args)  {
        //要通过反射去调用main方法,并传递参数。
        System.out.println(args[0]);
        System.out.println(args[1]);
    }
    @Test
    public void fun()throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
    {
        //1、得到当前类的Class
        Class clazz=Demo3.class;
        //2、得到这个类中main方法的Method
        Method main=clazz.getMethod("main", String[].class);
        //3、调用main方法
        //main.invoke(null, new Object[]{new String[]{"hello","world"}});
        main.invoke(null, (Object)(new String[]{"hello","world"}));
    }
}

invoke(Object 对象,Object….arg);
上面如果写成invoke(null,”hello”,”world”);会将hello,world包装到数组new String[]{“hello”,”world”};这时候arg的值就是一个数组。数组中有两个元素。它认为被调用的方法应该有两个参数。
invoke在将arg传递给args时,就将hello赋值给了args,而world传递给main方法的第二个参数,而main方法没有第二个参数,所以会报参数个数错误。所以第二个参数不能作为数组传递

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
反射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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值