通过反射获取类的基本结构,我们来讲解下如果通过反射获取类的构造方法,普通方法以及属性;
先把Student类改写下,加点方法;
package com.java1234.chap07.sec02; public class Student { private String name; private Integer age; public Student(String name) { super(); this.name = name; } public Student(String name, Integer age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public void say(){ System.out.println("我的姓名:"+name); } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
1,通过getConstructors()方法获取所有构造方法,测试类:
package com.java1234.chap07.sec02; import java.lang.reflect.Constructor; public class Test1 { public static void main(String[] args) { Class<?> c=null; try { c=Class.forName("com.java1234.chap07.sec02.Student"); System.out.println(c.getName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Constructor<?> cons[]=c.getConstructors(); for(Constructor<?> con:cons){ System.out.println("构造方法:"+con); } } }
运行输出:
com.java1234.chap07.sec02.Student
构造方法:public com.java1234.chap07.sec02.Student(java.lang.String)
构造方法:public com.java1234.chap07.sec02.Student(java.lang.String,java.lang.Integer)
2,通过getMethods()方法获取所有方法,测试类:
package com.java1234.chap07.sec02; import java.lang.reflect.Method; public class Test2 { public static void main(String[] args) { Class<?> c=null; try { c=Class.forName("com.java1234.chap07.sec02.Student"); System.out.println(c.getName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Method mds[]=c.getMethods(); for(Method m:mds){ System.out.println(m); } } }
运行输出:
com.java1234.chap07.sec02.Student
public java.lang.String com.java1234.chap07.sec02.Student.toString()
public java.lang.String com.java1234.chap07.sec02.Student.getName()
public void com.java1234.chap07.sec02.Student.setName(java.lang.String)
public java.lang.Integer com.java1234.chap07.sec02.Student.getAge()
public void com.java1234.chap07.sec02.Student.setAge(java.lang.Integer)
public void com.java1234.chap07.sec02.Student.say()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
3,通过getDeclaredFields()方法获取所有属性,测试类:
package com.java1234.chap07.sec02; import java.lang.reflect.Field; public class Test3 { public static void main(String[] args) { Class<?> c=null; try { c=Class.forName("com.java1234.chap07.sec02.Student"); System.out.println(c.getName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Field fs[]=c.getDeclaredFields(); for(Field f:fs){ System.out.println(f); } } }
运行输出:
com.java1234.chap07.sec02.Student
private java.lang.String com.java1234.chap07.sec02.Student.name
private java.lang.Integer com.java1234.chap07.sec02.Student.age