Java反射第三部分:方法调用

本文详细介绍了Java反射中Method对象的使用,包括访问方法、调用public非静态方法、非public非静态方法、静态方法,以及多态方法的调用。通过示例代码展示了如何获取和调用Method对象,同时强调了访问非public方法时需要设置setAccessible(true)。最后,解释了getMethods()与getDeclaredMethods()的区别,并指出反射调用方法遵循多态原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处:https://blog.youkuaiyun.com/jiyisuifeng222/article/details/117673524

本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 情花打雪 即可关注,每个工作日都有文章更新。

本篇文章主要讲解Java反射中Method字段对象的相关操作:

1.访问方法(Method)对象的方法汇总

2.调用public的非静态方法

3.调用非public的非静态方法

4.访问静态方法

5.多态方法的调用

6.区别和注意点

1.访问Method的方法汇总

以下四种方法在Class类可以返回关于字段的 Method 对象。

Method getMethod(name, Class...):获取某个public的Method(包括父类)

Method getDeclaredMethod(name, Class...):获取当前类的某个Method(不包括父类)

Method[] getMethods():获取所有public的Method(包括父类)

Method[] getDeclaredMethods():获取当前类的所有Method(不包括父类)

使用示例:

public class Main {

    public static void main(String[] args) throws Exception {

        Class stdClass = Student.class;

        // 获取public方法getScore,参数为String:
        System.out.println(stdClass.getMethod("getScore", String.class));

        // 获取继承的public方法getName,无参数:
        System.out.println(stdClass.getMethod("getName"));

        // 获取private方法getGrade,参数为int:
        System.out.println(stdClass.getDeclaredMethod("getGrade", int.class));

    }

}


class Student extends Person {

    public int getScore(String type) {
        return 99;
    }

    private int getGrade(int year) {
        return 1;
    }

}


class Person {

    public String getName() {
        return "Person";
    }

}

输出信息如下:

public int Student.getScore(java.lang.String)

public java.lang.String Person.getName()

private int Student.getGrade(int)

一个Method对象包含一个方法的所有信息:

  • getName():返回方法名称,例如:"getScore"
  • getReturnType():返回方法返回值类型,也是一个Class实例,例如:String.class
  • getParameterTypes():返回方法的参数类型,是一个Class数组,例如:{String.class, int.class}
  • getModifiers():返回方法的修饰符,它是一个int,不同的bit表示不同的含义。

2.调用public的非静态方法

import java.lang.reflect.Method;

public class MethodMain {

    public static void main(String[] args) throws Exception {

        //1.采用传统方法访问substring()方法
        String str1 = "Hello world1";

        String res1 = str1.substring(6); // "world1"

        System.out.println(res1);

        // 2.采用反射访问substring()方法
        String str2 = "Hello world2";

        // 获取public String substring(int beginIndex) 方法,参数为int类型:
        Method method = String.class.getMethod("substring", int.class);//这里需要传入int.class即参数的Class类

        // 在s对象上调用该方法并获取结果:
        String res2 = (String) method.invoke(str2, 6);//invoke(Object obj, Object... args)

        // 打印调用结果:
        System.out.println(res2);//"world2"

    }

}

注意到substring()有两个重载方法,我们获取的是String substring(int)这个方法。

Method实例调用invoke就相当于调用该方法,invoke的第一个参数是对象实例,即在哪个实例上调用该方法,后面的可变参数要与方法参数一致,否则将报错。

3.调用非public的非静态方法

public class Main {

    public static void main(String[] args) throws Exception {
        Person p = new Person();
        Method m = p.getClass().getDeclaredMethod("setName", String.class);
        //访问非public方法,需要在这里设置一下,否则会报错IllegalAccessException
        m.setAccessible(true);
        m.invoke(p, "Bob");
        System.out.println(p.name);
    }

}

class Person {
    String name;
    private void setName(String name) {
        this.name = name;
    }
}

4.访问静态方法

public class Main {

    public static void main(String[] args) throws Exception {

        // 获取Integer.parseInt(String)方法,参数为String:
        Method m = Integer.class.getMethod("parseInt", String.class);

        // 调用该静态方法并获取结果:
        Integer n = (Integer) m.invoke(null, "12345");//如果获取到的Method表示一个静态方法,调用静态方法时,由于无需指定实例对象,所以invoke方法传入的第一个参数永远为null。我们以Integer.parseInt(String)为例:

        // 打印调用结果:
        System.out.println(n);

    }

}

5.多态方法的调用

假设:一个Person类定义了hello()方法,并且它的子类Student也覆写了hello()方法,那么,从Person.class获取的Method,作用于Student实例时,调用的方法到底是哪个?

public class Main {

    public static void main(String[] args) throws Exception {

        // 获取Person的hello方法:
        Method h = Person.class.getMethod("hello");

        // 对Student实例调用hello方法:
        h.invoke(new Student());//输出:Student:hello
    }
}

class Person {
    public void hello() {
        System.out.println("Person:hello");
    }
}

class Student extends Person {
    public void hello() {
        System.out.println("Student:hello");
    }
}

运行上述代码,发现打印出的是Student:hello,因此,使用反射调用方法时,仍然遵循多态原则:即总是调用实际类型的覆写方法(如果存在)。
反射代码:

Method m = Person.class.getMethod("hello");

m.invoke(new Student());

相当于:

Person p = new Student();

p.hello();//多态

6.区别和注意点

getMethods()与getDeclaredMethods()区别

1.getMethods()返回所有可访问的公共方法,在类中声明或继承自父类。

2.getDeclaredMethods()获取当前类的所有方法(不包括父类)。但是可以是public或者private修饰的。注意:访问private修饰字段,需要设置method.setAccessible(true);避开检查。
 

关注我的技术公众号,每天都有优质技术文章推送。

微信扫一扫下方二维码即可关注:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值