java反射 - getXXX 與 getDeclaredXXX

來源:互聯網
上載者:User

標籤:nal   get   屬性   cep   chm   row   alt   ash   sse   

1、getXXX 和 getDeclaredXXX

java 裡 Class<?> 有下面這些方法:

類似的方法有:

2、getMethod(s) 和 getDeclaredMethod(s)

getDeclaredMethods只擷取當前對象申明的方法,不包含繼承過來的方法

* Returns an array containing {@code Method} objects reflecting all the
* declared methods of the class or interface represented by this {@code
* Class} object, including public, protected, default (package)
* access, and private methods, but excluding inherited methods.

getMethods擷取public方法

* Returns an array containing {@code Method} objects reflecting all the
* public methods of the class or interface represented by this {@code
* Class} object, including those declared by the class or interface and
* those inherited from superclasses and superinterfaces.

以常用的getMethod和getDeclaredMethod為例:

public class DemoService {    public String showDemoPublic(String methodName) {        return "show demo " + methodName;    }    protected String showDemoProtected(String methodName) {        return "show demo " + methodName;    }    private String showDemoPrivate(String methodName) {        return "show demo " + methodName;    }}

========================getMethods=========================
public java.lang.String com.logback.demo.service.DemoService.showDemoPublic(java.lang.String)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
========================getDeclaredMethods=========================
public java.lang.String com.logback.demo.service.DemoService.showDemoPublic(java.lang.String)
private java.lang.String com.logback.demo.service.DemoService.showDemoPrivate(java.lang.String)
protected java.lang.String com.logback.demo.service.DemoService.showDemoProtected(java.lang.String)

getMethods與getDeclaredMethods:

System.out.println("========================getMethods=========================");Method[] methods_1 = demoService.getClass().getMethods();for (Method m : methods_1) {    System.out.println(m);}System.out.println("========================getDeclaredMethods=========================");Method[] methods_2 = demoService.getClass().getDeclaredMethods();for (Method m : methods_2) {    System.out.println(m);}

getMethod與getDeclaredMethod

public void getMethodByName(@RequestParam("method") String method) throws NoSuchMethodException {try {       System.out.println("========================getMethod=========================");       Method method1 = demoService.getClass().getMethod("showDemo" + method, String.class);       System.out.println(method1);    } catch (Exception ex) {       System.out.println(ex);    }    try {      System.out.println("========================getDeclaredMethod=========================");      Method method2 = demoService.getClass().getDeclaredMethod("showDemo" + method, String.class);      System.out.println(method2);    } catch (Exception ex) {      System.out.println(ex);    }}

http://localhost:8088/getMethod?method=Public

========================getMethod=========================
public java.lang.String com.logback.demo.service.DemoService.showDemoPublic(java.lang.String)
========================getDeclaredMethod=========================
public java.lang.String com.logback.demo.service.DemoService.showDemoPublic(java.lang.String)

http://localhost:8088/getMethod?method=Protected

========================getMethod=========================
java.lang.NoSuchMethodException: com.logback.demo.service.DemoService.showDemoProtected(java.lang.String)
========================getDeclaredMethod=========================
protected java.lang.String com.logback.demo.service.DemoService.showDemoProtected(java.lang.String)

http://localhost:8088/getMethod?method=Private

========================getMethod=========================
java.lang.NoSuchMethodException: com.logback.demo.service.DemoService.showDemoPrivate(java.lang.String)
========================getDeclaredMethod=========================
private java.lang.String com.logback.demo.service.DemoService.showDemoPrivate(java.lang.String)

2、getField(s) 和 getDeclaredField(s)
public class Demo {private Integer id;private String name;    public String desc;}public class StudentDemo extends Demo {private Integer age;private Integer sex;}

驗證getField和getDeclareField

        for (Field field : demo.getClass().getDeclaredFields()) {            System.out.println(field);        }        System.out.println(">>>>>>>>");        for (Field field : demo.getClass().getFields()) {            System.out.println(field);        }        System.out.println("===========================================");        for (Field field : student.getClass().getDeclaredFields()) {            System.out.println(field);        }        System.out.println(">>>>>>>>");        for (Field field : student.getClass().getFields()) {            System.out.println(field);        }

getDeclaredField擷取所有申明的屬性,不包含繼承來的屬性。

getFields擷取所有public的屬性,包含繼承來的屬性。

private java.lang.Integer com.logback.demo.common.Demo.id

private java.lang.String com.logback.demo.common.Demo.name

public java.lang.String com.logback.demo.common.Demo.desc

>>>>>>>>

public java.lang.String com.logback.demo.common.Demo.desc

===========================================

private java.lang.Integer com.logback.demo.common.StudentDemo.age

private java.lang.Integer com.logback.demo.common.StudentDemo.sex

>>>>>>>>

 

public java.lang.String com.logback.demo.common.Demo.desc

getFields

java反射 - getXXX 與 getDeclaredXXX

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.