标签:
java.lang.Class;
java.lang.reflect.Constructor;
java.lang.reflect.Field;
java.lang.reflect.Method;
java.lang.reflect.Modifier;
public class Person { // Field private String person_name; protected int person_age; public boolean person_sex; // Construct public Person() { super(); } public Person(String name, int age, boolean sex) { super(); this.setPerson_name(name); this.person_age = age; this.person_sex = sex; } // Method public void AddAge(int year) { this.person_age += this.person_age + year; } public String getPerson_name() { return person_name; } public void setPerson_name(String person_name) { this.person_name = person_name; } }
public class Employee extends Person{ private double saray; protected String gangwei; public Employee() { super(); } public Employee(double saray, String gangwei) { super(); this.saray = saray; this.gangwei = gangwei; } public double getSaray() { return saray; } public void setSaray(double saray) { this.saray = saray; } public String getGangwei() { return gangwei; } public void setGangwei(String gangwei) { this.gangwei = gangwei; } public void addSaray(double money){ this.saray+=money; } }
1.根据类名构造一个实例
// 根据类名获取Class对象 Class c = Class.forName("Employee"); System.out.println(c.getName()); // 此处打印:Employee // 获取父类Class对象 Class cp = c.getSuperclass(); System.out.println(cp.getName());// 此处打印:Person // 获取类的访问修饰符 String modifiers = Modifier.toString(c.getModifiers()); System.out.println(modifiers); // 此处打印:public // 实例化 Object obj = c.newInstance(); //通过无参的构造函数进行实例化
Class c = Class.forName("Employee");
<span style="white-space:pre"> </span>/* * 获取类Employee的所有Field * * c.getField(name) c.getFields() c.getDeclaredFields() * c.getDeclaredField(name) */ Field[] fields = c.getDeclaredFields(); for (Field f : fields) { // 获取该Field的类型 Class type = f.getType(); // 获取该Field的名称 String name = f.getName(); // 获取该Field的访问修饰符 String sFieldModifiers = Modifier.toString(f.getModifiers()); System.out.println(sFieldModifiers + " " + type.getName() + " " + name + ";"); }
<span style="white-space:pre"> </span>/* * 获取类的构造函数 * c.getDeclaredConstructors() * c.getConstructors() * c.getDeclaredConstructor(parameterTypes) * c.getDeclaredConstructor(parameterTypes) */ Constructor[] constructors = c.getDeclaredConstructors(); for (Constructor cr : constructors) { // 构造函数名称 String name = cr.getName(); // 构造函数修饰符 String sConstructorModifiers = Modifier.toString(cr.getModifiers()); // 构造函数参数 Class[] paramTypes = cr.getParameterTypes(); String sParam = ""; for (int j = 0; j < paramTypes.length; j++) { if (j > 0) sParam += ", "; sParam += paramTypes[j].getName(); } System.out.println(sConstructorModifiers + " " + name + "(" + sParam + ");"); }
<span style="white-space:pre"> Method[] methods = c.getDeclaredMethods(); <span style="white-space:pre"> </span>for (Method m : methods) { <span style="white-space:pre"> </span>// 方法返回类型 <span style="white-space:pre"> </span>Class returnType = m.getReturnType(); <span style="white-space:pre"> </span>String sReturnType = returnType.toString(); <span style="white-space:pre"> </span>// 方法名称 <span style="white-space:pre"> </span>String name = m.getName(); <span style="white-space:pre"> </span>// 方法修饰符 <span style="white-space:pre"> </span>String sMethodModifiers = Modifier.toString(m.getModifiers()); <span style="white-space:pre"> </span>// 方法参数 <span style="white-space:pre"> </span>Class[] paramTypes = m.getParameterTypes(); <span style="white-space:pre"> </span>String sParam = ""; <span style="white-space:pre"> </span>for (int j = 0; j < paramTypes.length; j++) { <span style="white-space:pre"> </span>if (j > 0) <span style="white-space:pre"> </span>sParam += ", "; <span style="white-space:pre"> </span>sParam += paramTypes[j].getName(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>System.out.println(sMethodModifiers + " " + sReturnType + " " + name + "(" + sParam + ");"); <span style="white-space:pre"> </span>}</span>
<span style="white-space:pre"> </span>/* <span style="white-space:pre"> </span> * 动态给类的属性赋值 <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>Class c2 = Class.forName("Employee"); <span style="white-space:pre"> </span>Object e = c2.newInstance(); <span style="white-space:pre"> </span>Field f = c2.getDeclaredField("saray"); <span style="white-space:pre"> </span>// 因为属性saray是private的,所有需要调用setAccessible方法才能给该属性赋值,否则报错 <span style="white-space:pre"> </span>f.setAccessible(true); <span style="white-space:pre"> </span>f.set(e, 10000); <span style="white-space:pre"> </span>Field f2 = c2.getDeclaredField("gangwei"); <span style="white-space:pre"> </span>f2.set(e, "高级软件工程师"); <span style="white-space:pre"> </span>System.out.println(((Employee) e).getGangwei() + " " + ((Employee) e).getSaray()); <span style="white-space:pre"> </span>// 取值 <span style="white-space:pre"> </span>Field f3 = c2.getDeclaredField("saray"); <span style="white-space:pre"> </span>Field f4 = c2.getDeclaredField("gangwei"); <span style="white-space:pre"> </span>f3.setAccessible(true); <span style="white-space:pre"> </span>System.out.println(f3.get(e)); <span style="white-space:pre"> </span>System.out.println(f4.get(e));
高级软件工程师 10000.010000.0高级软件工程师
Class c3 = Class.forName("Employee"); Object e3 = c3.newInstance(); Field fSaray = c3.getDeclaredField("saray"); fSaray.setAccessible(true); fSaray.set(e3, 10000); System.out.println(fSaray.get(e3)); //输出10000 // 获取Method Method m = c3.getDeclaredMethod("addSaray", double.class); /* * Method m = c3.getDeclaredMethod("addSaray"); Method m2 = * c3.getDeclaredMethod("addSaray", double.class); * 如果有多个重名的方法,要添加参数让编译器知道应该调用哪个方法,这里只有一个参数,所以上面两种写法都可以 */ // 调用方法 m.invoke(e3, 5000); System.out.println(fSaray.get(e3)); //输出15000,因此证明方法调用成功输出结果如下:
10000.015000.0
标签:
原文地址:http://www.cnblogs.com/s502/p/5631634.html