本文主要是介绍JAVA反射获取类名,方法名,属性及构造方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//定义了个类,随便写了些属性,方法和构造方法
class ReflectTest {private String title;private int num;public String content;protected double income;public ReflectTest() {}private ReflectTest(String title, int num, double income) {this.title = title;this.num = num;this.income = income;}public ReflectTest(String title, int num) {this.title = title;this.num = num;}private void getSomeThing() {}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public double getIncome() {return income;}public void setIncome(double income) {this.income = income;}}
测试类
** * @description:* @author ldm* @date 2016-2-19 下午5:24:27* java中修饰符编号 :* PUBLIC: 1 PRIVATE: 2 PROTECTED: 4 STATIC: 8 FINAL: 16* SYNCHRONIZED: 32 VOLATILE: 64 TRANSIENT: 128 NATIVE: 256* INTERFACE: 512 ABSTRACT: 1024 STRICT: 2048*/
public class Test {@SuppressWarnings("rawtypes")public static void main(String[] args) {try {Class clazz = Class.forName("reflect.ReflectTest");System.out.println("------获取类名-----------");System.out.println(clazz.toString());// 获取类的完整名System.out.println(clazz.getSimpleName());// 获取类名System.out.println("------获取类中的构造方法-----------");// 获得所有构造方法Constructor[] cons = clazz.getDeclaredConstructors();for (int i = 0; i < cons.length; i++) {System.out.println("所有构造方法之" + cons[i].getName());// 方法修饰符System.out.println("修饰符类型:" + cons[i].getModifiers());}// 获得所有公共构造方法Constructor[] cons1 = clazz.getConstructors();for (int i = 0; i < cons1.length; i++) {System.out.println("公共构造方法之" + cons1[i].getName() + "--");System.out.println("修饰符类型:" + cons1[i].getModifiers());}System.out.println("-----获取类中定义的方法------------");Method[] mths = clazz.getDeclaredMethods();for (int i = 0; i < mths.length; i++) {System.out.println("所有方法之" + mths[i].getName());System.out.println("修饰符类型:" + mths[i].getModifiers());}Method[] mths1 = clazz.getMethods();for (int i = 0; i < mths1.length; i++) {System.out.println("公有方法之" + mths1[i].getName());System.out.println("修饰符类型:" + mths1[i].getModifiers());}System.out.println("-------获取所有的属性------------");Field[] fls = clazz.getFields();for (int i = 0; i < fls.length; i++) {System.out.println("公有属性之" + fls[i].getName());System.out.println("修饰符类型:" + fls[i].getModifiers());}Field[] fls1 = clazz.getDeclaredFields();for (int i = 0; i < fls1.length; i++) {System.out.println("所有属性之" + fls1[i].getName());System.out.println("修饰符类型:" + fls1[i].getModifiers());}}catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
这篇关于JAVA反射获取类名,方法名,属性及构造方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!