反射-getter/setter-java

 1 import java.lang.reflect.Method;
 2 
 3 /**
 4  * Created with IDEA
 5  * author:foreign
 6  * Date:2019/9/30
 7  * Time:10:40
 8  */
 9 public class ReflectionFk {
10     public static void main(String[] args) {
11         Class clazz = PersonFk.class;
12         Method[] methods = clazz.getMethods();
13         for (Method method : methods) {
14             if (isGetter(method)) {
15                 System.out.println("getter方法:" + method);
16             }
17             if (isSetter(method)) {
18                 System.out.println("setter方法:" + method);
19             }
20         }
21     }
22     //setter方法不一定有返回值
23     private static boolean isSetter(Method method) {
24         if (!method.getName().startsWith("set")) {
25             return false;
26         }
27         if (method.getParameterTypes().length != 1) {
28             return false;
29         }
30         return true;
31     }
32 
33     private static boolean isGetter(Method method) {
34         if (!method.getName().startsWith("get")) {
35             return false;
36         }
37         if (method.getParameterTypes().length != 0) {
38             return false;
39         }
40         if (void.class.equals(method.getReturnType())) {
41             return false;
42         }
43         return true;
44     }
45 }

 

转载于:https://www.cnblogs.com/fangke/p/11611812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值