您的位置:首页 > 编程语言 > Java开发

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

2017-06-01 13:50 381 查看
转载于:http://blog.csdn.net/gao_chun/article/details/42875575

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

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

描述

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

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

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

声明

[java]
view plain
copy

print?

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



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()方法

[java]
view plain
copy

print?

package com.app.ui;  
  
import java.lang.reflect.*;  
  
public class ClassDemo {  
  
   public static void main(String[] args) {  
      
     ClassDemo cls = new ClassDemo();  
     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());  
     }  
   }  
  
  
   private Integer show() {  
      return 1;  
   }  
      
   public void showInteger(Integer i) {  
      this.i = i;  
   }  
   public int i = 78655;  
}  



package com.app.ui;

import java.lang.reflect.*;

public class ClassDemo {

public static void main(String[] args) {

ClassDemo cls = new ClassDemo();
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());
}
}

private Integer show() {
return 1;
}

public void showInteger(Integer i) {
this.i = i;
}
public int i = 78655;
}


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

[java]
view plain
copy

print?

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



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


注:

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

getMethod () 获取的是类的所有共有方法,这就包括自身的所有public方法,和从基类继承的、从接口实现的所有public方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: