网站开发兼容问题,资讯类网站建设资质要求,软件网站开发设计,网络优化网站 site本文以我以前的文章为基础 。 在本文中#xff0c;我们将看到如何使用Java Reflection检索类相关信息。 我们将重点介绍方法名称。 注意#xff1a;我将创建一个单独的反射器实用程序类#xff0c;在该类中#xff0c;我们在其构造函数中输入一个目标类#xff0c;然后使… 本文以我以前的文章为基础 。 在本文中我们将看到如何使用Java Reflection检索类相关信息。 我们将重点介绍方法名称。 注意我将创建一个单独的反射器实用程序类在该类中我们在其构造函数中输入一个目标类然后使用一个单独的方法检索信息。 这样我们可以隔离我们的需求。 在开始之前请先查看此内容 。 如何在一个类中获取所有声明的方法名称 这意味着我们将获得在类内部声明的方法名称公共私有默认受保护即不是继承的方法。 public String[] getAllOwnMethodNames(){ArrayListString allMethods new ArrayListString();for(Method aMethod : myClass.getDeclaredMethods()){ allMethods.add(Method Name : aMethod.getName() , Full Name : aMethod.toString());}return allMethods.toArray(new String[allMethods.size()]);} 如何从一个类包括其自己的超类接口的继承的实现的方法中访问所有方法名称 public String[] getAllPubliAndInheritedMethodNames(){ArrayListString allMethods new ArrayListString();for(Method aMethod : myClass.getMethods()){ allMethods.add(Method Name : aMethod.getName() , Full Name : aMethod.toString());}return allMethods.toArray(new String[allMethods.size()]);} 注意要获得详细信息我们使用getName和toString方法。 对于这两种情况我们都可以指定方法名称来获取该特定方法。 myClass.getDeclaredMethod(Name of the method as string, parameter of that method)
myClass.getMethod(Name of the method as string, parameter of that method) 在这两种情况下我们都需要知道方法的名称。 有时对于一个类我们需要知道某个方法是Getter还是setter方法。 我们可以应用一个小的字符串过滤器如下所示 要知道它是否是Getter方法 aMethod.getName().startsWith(set); 要知道它是否是一个Setter方法 aMethod.getName().startsWith(get);翻译自: https://www.javacodegeeks.com/2015/01/how-to-get-all-method-information-under-a-class-in-java-reflection.html