getDeclaredMethod 和 getMethod 的区别

半兽人 发表于: 2018-01-10   最后更新时间: 2018-01-13 16:37:21  
{{totalSubscript}} 订阅, 5,372 游览

getDeclaredMethod()获取的是类自身声明的所有方法,包含public、protected和private方法。getMethod()获取的是类的所有共有方法,这就包括自身的所有public方法,和从基类继承的、从接口实现的所有public方法。

实例一

getDeclaredMethod 和 getMethod 的区别

package compackage com.system.net.reflect;

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

publicage comnetct;

importmport java.lang.reflect.InvocationTargetException;
import java.langpackage com.system.net.reflect;

import java.lang.reflect.InvocationTargetException;
importe comsystem.net.reflect;

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

public class Say {

    public void say(String methodStr, String name, int age) throws SecurityException, IllegalArgumentException, NoSuchMethodExceptionpackage comm.system.netflect;

import javaflect.InvocationTargetExceptionpackagepackage com.system.net.reflect;

importpackage com.system.net.reflect;

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

public class Say {

    public void say(Stringcomstem.netect;

importect.InvocationTargetExceptionvocationTargetException;
import java.lang.reflect.Method;

public classpackage com.system.net.reflect;

import java.lang.reflect.InvocationTargetException;
import java.langcomt.reflectpackage com.system.net.reflect;

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

public class Say {

    public void say(String methodStr, String name, int age) throws SecurityException, IllegalArgumentException, NoSuchMethodExceptionpackage com.system.net.reflect;

import java.langpackage com.system.net.reflect;

import java.lang.reflect.InvocationTargetException;
importpackage comtem.netreflect;

import java.lang.reflect.InvocationTargetException;
importpackage com.system.net.reflect;

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

public class Say {

    public void say(String methodStrpackage comsystem.netect;

import

importng.reflectpackage com.system.net.reflect;

import javapackage com.system.net.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflectpackage comtem.netpackage com.system.net.reflect;

import javapackage com.system.net.reflect;

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

public class Say {

    public void saypackage com.system.netect;

import javapackage com.system.net.reflect;

import java.lang.reflect.InvocationTargetException;
import javapackage com.system.net.reflect;

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

public class Say {

    public voidpackage comystem.net.reflectang.reflecteflect.InvocationTargetException;
import java.lang.reflect.Method;

publicpackage com.system.net.reflect;

import java.lang.reflect.InvocationTargetException;
import javapackage com.system.net.reflect;

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

public class Say {

    public void say(String methodStr, String name, int age) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetExceptionpackagecomstem.nett.reflectect;

importport javaa.langeflect.InvocationTargetExceptionpackage com.system.net.reflect;

import java.lang.reflect.InvocationTargetException;
importcompackage com.system.net.reflect;

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

public class Say {

    public void say(Stringpackage comystem.netnet.reflect javaang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Say {

    public void saypackage com.system.net.reflect;

import java.lang.reflectpackage com.system.net.reflect;

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

public class Say {

    public void say(String methodStrpackagecomsystemstem.net.reflect;

import javaect.InvocationTargetExceptionpackage com.system.net.reflect;

import java.langpackage com.system.net.reflect;

import java.lang.reflect.InvocationTargetException;
import java.langcom.system.net.reflect;

import java.lang.reflect.InvocationTargetExceptionpackage com.system.net.reflect;

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

public class Say {

    public void say(String methodStr, String name, int age) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InvocationTargetException {
        //  Method method = this.getClass().getMethod("say" + methodStr, new Class[]{String.class,int.class});              //1
        Method method = this
        Method method = this.getClass().getDeclaredMethod("say" + methodStr, new Class[]{String.class, int.class});      //2
        method.invoke     methodoke(this
        method.invoke(this, new Object[]{name, age});
    }

    public void sayHello(String name
        method.invoke(this, new Object[]{name, age});
    }

    public void sayHello(String name, int age) {
        System.out
        method.invoke(this, new Object[]{name, age});
    }

    public void sayHello(String name, int age) {
        System     method.invoke
        method.invoke(this, new Object[]{name, age});
    }

    public void sayHello(String name, int age
        method.invoke(this, new Object[]{name, age});
    }

    public void sayHello(String name, int age) {
        System.out.println("hello " + name + ",I know you are " + age}

    protectedrotected voidHi(String age    System.out.println"hi " + name",I know you are " + age);
    }

    private void sayBye(String name + age);
    }

    private voidayBye(String + age);
    }

    private void sayBye(String name, int age) {
        System.out.println("bye " + name",I know you are " + age);
    }

    public staticge);
    }

    public main + age);
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        new Say().say("Hello", "name1", 22);
        new"Hi", "name2", 22);
    }
}

当使用say方法里的 //1 注解语句时,main函数抛出异常,可见使用getMethod时,因为sayHi是保护方法,获取方法sayHi失败

hello jianjianjiao,I know you are 22
Exception in thread "main" java.lang.NoSuchMethodException: pkg.reflection.Say.sayHi(java.lang.String, int)
    at java.lang.Class.getMethod(Unknown Source)
    at pkg.reflection.Say.say(Say.java:9)
    at pkg.reflection.SayTest.main(SayTest.java:27)

当使用say方法里的//2语句时,可以正常调用

hello jianjianjiao,I know you are 22
hi jianjianjiao,I know you are 22

可见,不能用java.lang.Class.getMethod方法获取自身的非public方法,用java.lang.Class.getDeclaredMethod方法可以。

实例二

getDeclaredMethods 和 getMethods的区别

package com.system.net.reflect;

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

public class ReflectionUtils {

    public static void getMethodDeclaration(Class<?> clazz) {
        Method[] methods = clazz.getDeclaredMethods();
        System.out.println("MethodDeclaration in " + clazz.getName());
        for (Method method : methods) {
            method.setAccessible(true);
            System.out.println(method.getName());
        }
    }

    public static void getMethod(Class<?> clazz) {
        Method[] methods = clazz.getMethods();
        System.out.println("Method in " + clazz.getName());
        for (Method method : methods) {
            System.out.println(method.getName());
        }
    }

    public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        ReflectionUtils.getMethod(Say.class);
        ReflectionUtils.getMethodDeclaration(Say.class);
    }
}

输出:

Method in pkg.reflection.Say
say
sayHello
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
MethodDeclaration in pkg.reflection.Say
say
sayHello
sayHi
sayBye

可见

  • getMethods 方法输出的是自身的public方法和父类Object的public方法。
  • getDeclaredMethods方法输出的是自身的public、protected、private方法。

项目已分享到git上: https://github.com/orchome/net

更新于 2018-01-13

查看java更多相关的文章或提一个关于java的问题,也可以与我们一起分享文章