java method getdeclaredmethod_java学习(1) ----getMethod()和getDeclaredMethod()的区别(转)

本文详细介绍了Java中getMethod()和getDeclaredMethod()方法的区别,包括它们获取的范围(public、所有)、使用场景及实例。重点讲解了如何通过这两个方法获取继承父类的public方法,并提供了编译示例。
摘要由CSDN通过智能技术生成

转自: https://blog.csdn.net/qq_36443736/article/details/82890011

getMethod():获取自身能用所有的public公共方法。1.类本身的public 2.继承父类的public 3.实现接口的public

getDeclaredMethod():获取类自身声明的所有方法,包含public、protected和private方法。。

getMethod()获取继承父类的public方法举例:

a1ee690623568eaac418313ab5aa97d1ca5.jpg

a8d612af64064d71d4904f6dcf911cc2a94.jpg

public classFather {publicFather() {

System.out.println("调用了父类构造方法");

}public voidfatherSay() {

System.out.println("我是爸爸");

}

}public class Son extendsFather {publicSon() {//TODO Auto-generated constructor stub

System.out.println("调用了子类构造方法");

}public voidsonSay() {

System.out.println("我是儿子");

}public static voidmain(String[] args) {

Son son=newSon();

son.fatherSay();

}

}

View Code

测试类:

3c896050a8d0e4e3e9ec40268b008b01a75.jpg

309c4d0add40f253aa89e271889c080a26f.jpg

public classtest1 {public static voidmain(String[] args) {

Class clazz=Son.class;try{//报错 NoSuchMethodException

Method method =clazz.getDeclaredMethod("fatherSay");

method.invoke(clazz.newInstance());//运行结果://调用了父类构造方法//调用了子类构造方法//我是爸爸

Method method2 =clazz.getMethod("fatherSay");

method2.invoke(clazz.newInstance());

}catch(SecurityException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(NoSuchMethodException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IllegalArgumentException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IllegalAccessException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(InvocationTargetException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(InstantiationException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

View Code

java.lang.Class.getDeclaredMethod()方法用法

注:方法返回一个Method对象,它反映此Class对象所表示的类或接口的指定已声明方法。

描述

java.lang.Class.getDeclaredMethod()方法返回一个Method对象,它反映此Class对象所表示的类或接口的指定已声明方法。

name 参数是一个字符串,指定所需的方法的简单名称,

parameterTypes 参数是一个数组的Class对象识别方法的形参类型,在声明的顺序

声明

public Method getDeclaredMethod(String name, Class... parameterTypes) throws NoSuchMethodException,SecurityException

参数

name -- 方法的名称

parameterTypes -- 参数数组

返回值

匹配指定名称和参数的类的方法,此方法返回的Method对象

异常

NoSuchMethodException -- 如果匹配方法未找到

NullPointerException -- 如果name 为 null.

SecurityException -- If a security manager, s, is present.

实例

如何使用java.lang.Class.getDeclaredMethod()方法

packagecom.app.ui;import java.lang.reflect.*;public classClassDemo {public static voidmain(String[] args) {

ClassDemo cls= newClassDemo();

Class c=cls.getClass();try{//parameter type is null

Method m = c.getDeclaredMethod("show", null);

System.out.println("method = " +m.toString());//method Integer

Class[] cArg = new Class[1]

cArg[0] = Integer.class;

Method lMethod= c.getDeclaredMethod("showInteger", cArg);

System.out.println("method = " +lMethod.toString());

}catch(NoSuchMethodException e){

System.out.println(e.toString());

}

}privateInteger show() {return 1;

}public voidshowInteger(Integer i) {this.i =i;

}public int i = 78655;

}

编译和运行程序,产生以下结果:

method = private java.lang.Integer ClassDemo.show()

method = public void ClassDemo.showInteger(java.lang.Integer)

invoke调用类中的方法,最简单的用法是可以把方法参数化

invoke(class, method)

比如你Test类里有一系列名字相似的方法setValue1、setValue2等等

可以把方法名存进数组v[],然后循环里invoke(test,v[i]),就顺序调用了全部setValue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值