/**
*
*/
package cn.thcic;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
/**
* java反射
* <p>
*
* </p>
* by Zhiwang Zhang on 2014年7月21日
*/
public class Test110 {
private int age;
private String name;
private boolean flag;
public String addr;
public Date time;
public Long id;
private Test110(int age) {
super();
this.age = age;
}
private Test110(String name) {
super();
this.name = name;
}
public Test110(boolean flag) {
super();
this.flag = flag;
}
public Test110(Date time) {
super();
this.time = time;
}
public Test110(int age, String name, boolean flag, String addr, Date time,
Long id) {
super();
this.age = age;
this.name = name;
this.flag = flag;
this.addr = addr;
this.time = time;
this.id = id;
}
public Test110() {
super();
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
return "Test110 [age=" + age + ", name=" + name + ", flag=" + flag
+ ", addr=" + addr + ", time=" + time + ", id=" + id + "]";
}
public int eat(int age, String name) {
System.out.println("eat()方法");
return 100;
}
private void sleep() {
System.out.println("sleep()方法");
}
public static void main(String[] args) {
// 一、在Test110实例化之前获得Test110类的信息的两种方法:
// 1、
Class classInfo = Test110.class;
// 2、
try {
Class classSec = Class.forName("cn.thcic.Test110");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 二、通过反射实例化Test110
// 1、通过Class类的newInstance()方法,这种方法只能调用Test110默认的无参的构造方法
try {
Test110 test = (Test110) classInfo.newInstance();
System.out.println("I'm test." + test.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 2、通过Class类先得到Constructor类(封装类的构造方法),再通过Constructor类的newInstance()方法获得Test110的实例,这种方法可以调用Test110的有参构造方法
// 通过Class类得到Constructor类有四种方法:
// (1)getConstructors()方法,得到Test110类的所有被public修饰的构造方法
Constructor[] constructors = classInfo.getConstructors();
System.out
.println("通过getConstructors()方法,得到Test110类的所有被public修饰的构造方法,这些构造方法共有:"
+ constructors.length + "个," + "它们是:");
if (constructors != null) {
for (Constructor con : constructors) {
System.out.println(con.toString());
}
}
// 用其中某一个构造方法去实例化
try {
Test110 testThir = (Test110) constructors[0]
.newInstance(new Date());
System.out.println("I'm testThir." + testThir.toString());
} catch (Exception e) {
e.printStackTrace();
}
// (2)getConstructor(Class... parameterTypes)方法,返回一个 Constructor 对象,它反映此
// Class 对象所表示的类的指定公共构造方法。
try {
Constructor constructor = classInfo.getConstructor(boolean.class);
Test110 testSec = (Test110) constructor.newInstance(true);
System.out.println(testSec.flag);
} catch (Exception e) {
e.printStackTrace();
}
// (3)getDeclaredConstructors()方法,得到Test110所有的构造方法(包括私有的)
Constructor[] constructorsSec = classInfo.getDeclaredConstructors();
System.out
.println("通过getDeclaredConstructors()方法,得到Test110类所有的构造方法,这些构造方法共有:"
+ constructorsSec.length + "个," + "它们是:");
if (constructorsSec != null) {
for (Constructor con : constructorsSec) {
System.out.println(con.toString());
}
}
// (4)getDeclaredConstructor(Class... parameterTypes)方法,返回一个 Constructor
// 对象,该对象反映此 Class 对象所表示的类或接口的指定构造方法
try {
Constructor constructorThir = classInfo
.getDeclaredConstructor(int.class);
Test110 testThir = (Test110) constructorThir.newInstance(1);
System.out.println("I'm testThir." + testThir.toString());
} catch (Exception e) {
e.printStackTrace();
}
// 三、通过反射得到Test110的成员变量
// Field类,封装类的成员变量(域)
// 通过Class类得到Field类有四种方法(类似于通过Class类得到Constructor类有四种方法):
// 1、
Field[] fields = classInfo.getFields();
System.out.println("Test110共有公共域" + fields.length + "个,它们是:");
if (fields != null) {
for (Field field : fields) {
System.out.println(field.toString());
}
}
Test110 testFourth = null;
try {
testFourth = (Test110) classInfo.newInstance();
testFourth.addr = "Beijing";
// get(Object obj)方法,返回指定对象上此 Field 表示的字段的值。
System.out.println("TestFourth lives in "
+ fields[0].get(testFourth));
} catch (Exception e) {
e.printStackTrace();
}
// 2、
try {
Field fieldForTime = classInfo.getField("time");
testFourth.setTime(new Date());
System.out.println("currentTime:" + fieldForTime.get(testFourth));
} catch (Exception e) {
e.printStackTrace();
}
// 3、
Field[] fieldsSec = classInfo.getDeclaredFields();
System.out.println("Test110的所有域" + fieldsSec.length + "个,它们是:");
if (fieldsSec != null) {
for (Field field : fieldsSec) {
System.out.println(field.toString());
}
}
try {
testFourth.setAge(100);
System.out.println("TestFourth is " + fieldsSec[0].get(testFourth)
+ " years old.");
} catch (Exception e) {
e.printStackTrace();
}
// 4、
try {
Field fieldForName = classInfo.getDeclaredField("name");
testFourth.name = "aaa";
System.out.println("TestFourth's name is "
+ fieldForName.get(testFourth));
} catch (Exception e) {
e.printStackTrace();
}
// 四、通过反射得到Test110的方法
// Method类,封装类的方法
// 通过Class类得到Method类有四种方法(类似于通过Class类得到Constructor类有四种方法):
// 1、
Method[] methods = classInfo.getMethods();
System.out.println("Test110共有公共方法" + methods.length + "个,它们是:");
if (methods != null) {
for (Method method : methods) {
System.out.println(method.toString());
}
}
// 得到某一具体方法:
try {
System.out.println(methods[6].invoke(testFourth, 1, ""));
} catch (Exception e) {
e.printStackTrace();
}
// 2、
try {
Method methodForEat = classInfo.getMethod("eat", int.class,
String.class);
System.out.println(methodForEat.invoke(testFourth, 100, null));
} catch (Exception e) {
e.printStackTrace();
}
// 3、
Method[] methodsSec = classInfo.getDeclaredMethods();
System.out.println("Test110共有方法" + methodsSec.length + "个,它们是:");
if (methodsSec != null) {
for (Method method : methodsSec) {
System.out.println(method);
}
}
// 4、
try {
Method methodForSleep = classInfo.getDeclaredMethod("sleep");
methodForSleep.invoke(testFourth);
} catch (Exception e) {
e.printStackTrace();
}
}
}