标签:
反射机制是Java语言中一个非常重要的特性,它允许程序在运行时进行自我检查,同时也允许其对内部成员进行操作。由于反射机制能够实现在运行时对类进行装载,因此能够增加程序的灵活性,但是不恰当地使用反射机制,也会严重影响系统的性能。具体而言,反射机制的主要功能主要有:①得到一个对象所属的类,②获取一个类的所有成员变量和方法,③在运行时创建对象,调用对象的方法。
反射机制获取类的方法有三种,我们分别展示如何获取Reflection包下的Student类
Class类中的6个重要方法
public class Test { public static void main(String[] args) { //Class Student = Class.forName("Reflection.Student"); //包名+类名 Class Student = Student.class; Field[] fs = Student.getDeclaredFields(); for(Field field : fs){ System.out.print(field.getModifiers() + " "); ////获得属性的修饰符,例如public,static等等,结果为int型 System.out.print(field.getType().getSimpleName() + " "); //获取属性类型的名称 System.out.println(field.getName()); } } } class Student { public String name; private int id; public double score; public static String SCHOOLNAME = "Beijing University of Post and Telecommunications"; public void printName(){ System.out.println(name); } public void setID(int ID){ this.id = ID; } public static void print(){ System.out.println("This is a Student class"); } }
输出结果如下:
1 String name
2 int id
1 double score
9 String SCHOOLNAME
public static void main(String[] args) { //Class Student = Class.forName("Reflection.Student"); //包名+类名 Class Student = Student.class; try { Field fs = Student.getDeclaredField("id"); Object o = Student.newInstance(); fs.setAccessible(true); //Student对象的id属性为private,只有设置为true才允许在外部直接修改属性值 fs.set(o, 2018111546); System.out.println(fs.get(o)); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
输出结果:
2018111546
public class Test { public static void main(String[] args) { //Class Student = Class.forName("Reflection.Student"); //包名+类名 Class Student = Student.class; Constructor[] con = Student.getConstructors(); for(Constructor c : con) System.out.println(c.toString());
Constructor c = Student.getConstructor(String.class, int.class, double.class);
System.out.println(c.toString()); } } class Student { public String name; private int id; public double score; public static String SCHOOLNAME = "Beijing University of Post and Telecommunications"; public Student(){ name = "zhangsan"; id = 2018111846; score = 86; } public Student(String name, int id, double score){ this.name = name; this.id = id; this.score = score; } public void printName(){ System.out.println(name); } public void setID(int ID){ this.id = ID; } public static void print(){ System.out.println("This is a Student class"); } }
输出结果
public Reflection.Student()
public Reflection.Student(java.lang.String,int,double)
public Reflection.Student(java.lang.String,int,double)
public static void main(String[] args) { //Class Student = Class.forName("Reflection.Student"); //包名+类名 Class Student = Student.class; Method[] mds = Student.getMethods(); for(Method m : mds){ System.out.println(m.getReturnType()); Class[] classs = m.getParameterTypes(); for(Class s : classs) System.out.println(s.getName()); } } 输出结果: void void void int void void long int void long int class java.lang.Class boolean java.lang.Object class java.lang.String void void
标签:
原文地址:http://www.cnblogs.com/zhanglei93/p/5887801.html