java.lang.Class.getDeclaredMethod()方法
java.lang.Class.getDeclaredMethod()方法用法实例教程 - 方法返回一个Method对象,它反映此Class对象所表示的类或接口的指定已声明方法。
width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="box-sizing: border-box; left: 0px; position: absolute; top: 0px;">
id&#61;"iframeu1064372_0" src&#61;"http://pos.baidu.com/dchm?rdid&#61;1064372&dc&#61;2&di&#61;u1064372&dri&#61;0&dis&#61;0&dai&#61;1&ps&#61;325x409&dcb&#61;BAIDU_SSP_define&dtm&#61;BAIDU_DUP_SETJSONADSLOT&dvi&#61;0.0&dci&#61;-1&dpt&#61;none&tsr&#61;0&tpr&#61;1463996464998&ti&#61;java.lang.Class.getDeclaredMethod()%E6%96%B9%E6%B3%95&ari&#61;1&dbv&#61;2&drs&#61;1&pcs&#61;1305x604&pss&#61;1305x1464&cfv&#61;0&cpl&#61;4&chi&#61;1&cce&#61;true&cec&#61;UTF-8&tlm&#61;1463996464<u&#61;http%3A%2F%2Fwww.yiibai.com%2Fjavalang%2Fclass_getdeclaredmethod.html<r&#61;https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3Djk0O5do5u_s4m3ABGo99XO7NBe4HIvqKSZRSKarsxVp3QSwBhn14kksECRIiYzO7gi0mXSmkLQwXHYbm6knxbSUBs4W39rKAqk4b1kjRxga%26wd%3D%26eqid%3Db46f7266000063d3000000055742cfec&ecd&#61;1&psr&#61;1366x768&par&#61;1366x728&pis&#61;-1x-1&ccd&#61;24&cja&#61;false&cmi&#61;6&col&#61;zh-CN&cdo&#61;-1&tcn&#61;1463996465&qn&#61;201a2dc1319f1ce9&tt&#61;1463996464979.26.165.169" width&#61;"728" height&#61;"90" align&#61;"center,center" vspace&#61;"0" hspace&#61;"0" marginwidth&#61;"0" marginheight&#61;"0" scrolling&#61;"no" frameborder&#61;"0" allowtransparency&#61;"true" style&#61;"box-sizing: border-box; border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">
描述
java.lang.Class.getDeclaredMethod()方法返回一个Method对象&#xff0c;它反映此Class对象所表示的类或接口的指定已声明方法。name参数是一个字符串&#xff0c;指定所需的方法的简单名称&#xff0c;parameterTypes参数是一个数组的Class对象识别方法的形参类型&#xff0c;在声明的顺序
声明
以下是java.lang.Class.getDeclaredMethod()方法的声明
public Method getDeclaredMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException
参数
-
name -- 方法的名称
-
parameterTypes -- 参数数组
返回值
匹配指定名称和参数的类的方法&#xff0c;此方法返回的Method对象
异常
-
NoSuchMethodException -- 如果匹配方法未找到
-
NullPointerException -- 如果name 为 null.
-
SecurityException -- If a security manager, s, is present.
实例
下面的例子说明了如何使用java.lang.Class.getDeclaredMethod()方法
package com.yiibai;import java.lang.reflect.*;public class ClassDemo {public static void main(String[] args) {ClassDemo cls &#61; new ClassDemo();Class c &#61; cls.getClass();try { // parameter type is nullMethod m &#61; c.getDeclaredMethod("show", null);System.out.println("method &#61; " &#43; m.toString()); // method IntegerClass[] cArg &#61; new Class[1];cArg[0] &#61; Integer.class;Method lMethod &#61; c.getDeclaredMethod("showInteger", cArg);System.out.println("method &#61; " &#43; lMethod.toString());}catch(NoSuchMethodException e) {System.out.println(e.toString());}}private Integer show() {return 1;}public void showInteger(Integer i) {this.i &#61; i;}public int i &#61; 78655;
}
让我们来编译和运行上面的程序&#xff0c;这将产生以下结果&#xff1a;
method &#61; private java.lang.Integer ClassDemo.show()
method &#61; public void ClassDemo.showInteger(java.lang.Integer