java反射getfield_java反射 - getXXX 与 getDeclaredXXX

1、getXXX 和 getDeclaredXXX

java 里 Class> 有下面这些方法:

35f0eca875e0a5a109df7ab64d379646.png

类似的方法有:

8420f293a3a9a2fe0c92167efed670cd.png

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 classDemoService {publicString showDemoPublic(String methodName) {return "show demo " +methodName;

}protectedString showDemoProtected(String methodName) {return "show demo " +methodName;

}privateString showDemoPrivate(String methodName) {return "show demo " +methodName;

}

}

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);

}

========================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)

getMethod与getDeclaredMethod

public void getMethodByName(@RequestParam("method") String method) throwsNoSuchMethodException {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 classDemo {privateInteger id;privateString name;publicString desc;

}public class StudentDemo extendsDemo {privateInteger age;privateInteger 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

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页
评论

打赏作者

JENNIFER LU

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值