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:" + Method); 16 } . 17 IF (isSetter (Method)) { 18 is System.out.println ( "the setter Method:" + Method); . 19 } 20 is } 21 is } 22 is / / the setter method does not necessarily have a return value 23 is Private static Boolean isSetter (method, method) { 24 IF (method.getName () startsWith ( "SET"!. )) { 25 return to false ; 26 is } 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 }