轉(zhuǎn)自:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html
- JAVA反射機(jī)制是在運(yùn)行狀態(tài)中钢悲,對于任意一個類屹耐,都能夠知道這個類的所有屬性和方法泛粹;對于任意一個對象胜臊,都能夠調(diào)用它的任意一個方法和屬性匣掸;這種動態(tài)獲取的信息以及動態(tài)調(diào)用對象的方法的功能稱為 java 語言的反射機(jī)制霞揉。
- Java 上指的是可以于運(yùn)行時加載竹海、探知、使用編譯期間完全未知的 classes彼绷。換句話說巍佑,Java 程序可以加載一個運(yùn)行時才得知名稱的 class,獲悉其完整構(gòu)造(但不包括 methods 定義)寄悯,并生成其對象實(shí)體萤衰、或?qū)ζ?fields 設(shè)值、或喚起其 methods猜旬。
1. 通過一個對象獲得完整的包名和類名
package Reflect;
class Demo{
//other codes...
}
class hello{
public static void main(String[] args) {
Demo demo=new Demo();
System.out.println(demo.getClass().getName());
}
}
【運(yùn)行結(jié)果】:
Reflect.Demo
2. 實(shí)例化 Class 類對象
package Reflect;
class Demo{
//other codes...
}
class hello{
public static void main(String[] args) {
Class<?> demo1=null;
Class<?> demo2=null;
Class<?> demo3=null;
try{
//一般盡量采用這種形式
demo1=Class.forName("Reflect.Person");
}catch(Exception e){
e.printStackTrace();
}
demo2=new Demo().getClass();
demo3=Demo.class;
System.out.println("類名稱 "+demo1.getName());
System.out.println("類名稱 "+demo2.getName());
System.out.println("類名稱 "+demo3.getName());
}
}
【運(yùn)行結(jié)果】:
類名稱 Reflect.Demo
類名稱 Reflect.Demo
類名稱 Reflect.Demo
3. 通過 Class 實(shí)例化其他類的對象(調(diào)用無參構(gòu)造函數(shù))
package Reflect;
class Demo{
//other codes...
}
class Person{
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString(){
return "["+this.name+" "+this.age+"]";
}
private String name;
private int age;
}
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try{
demo=Class.forName("Reflect.Person");
}catch (Exception e) {
e.printStackTrace();
}
Person per=null;
try {
per=(Person)demo.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
per.setName("Rollen");
per.setAge(20);
System.out.println(per);
}
}
【運(yùn)行結(jié)果】:
[Rollen 20]
- 但是要注意脆栋,當(dāng)把 Person 中的默認(rèn)的無參構(gòu)造函數(shù)取消的時候,比如自己只定義一個有參數(shù)的構(gòu)造函數(shù)之后洒擦,會出現(xiàn)錯誤椿争。
所以在編寫使用 Class 實(shí)例化其他類的對象的時候,一定要記得定義無參的構(gòu)造函數(shù)熟嫩。
4. 通過 Class 調(diào)用其他類中的構(gòu)造函數(shù)
package Reflect;
class Demo{
//other codes...
}
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "[" + this.name + " " + this.age + "]";
}
public Person() {
}
public Person(String name) {
this.name = name;
}
public Person(int age) {
this.age = age;
}
public Person(String name, int age) {
this.age = age;
this.name = name;
}
}
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try{
demo=Class.forName("Reflect.Person");
}catch (Exception e) {
e.printStackTrace();
}
Person per1=null;
Person per2=null;
Person per3=null;
Person per4=null;
//取得全部的構(gòu)造函數(shù)
Constructor<?> cons[]=demo.getConstructors();
try{
per1=(Person)cons[0].newInstance();
per2=(Person)cons[1].newInstance("Rollen");
per3=(Person)cons[2].newInstance(20);
per4=(Person)cons[3].newInstance("Rollen",20);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(per1);
System.out.println(per2);
System.out.println(per3);
System.out.println(per4);
}
}
【運(yùn)行結(jié)果】:
[null 0]
[Rollen 0]
[null 20]
[Rollen 20]
5. 返回一個類實(shí)現(xiàn)的接口
package Reflect;
class Demo{
//other codes...
}
interface China{
public static final String name="Rollen";
public static int age=20;
public void sayChina();
public void sayHello(String name, int age);
}
class Person implements China{
public Person() {
}
public Person(String sex){
this.sex=sex;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public void sayChina(){
System.out.println("hello ,china");
}
@Override
public void sayHello(String name, int age){
System.out.println(name+" "+age);
}
private String sex;
}
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try{
demo=Class.forName("Reflect.Person");
}catch (Exception e) {
e.printStackTrace();
}
//保存所有的接口
Class<?> intes[]=demo.getInterfaces();
for (int i = 0; i < intes.length; i++) {
System.out.println("實(shí)現(xiàn)的接口 "+intes[i].getName());
}
}
}
【運(yùn)行結(jié)果】:
實(shí)現(xiàn)的接口 Reflect.China
6. 取得其他類中的父類
package Reflect;
class Demo{
//other codes...
}
class Person implements China{
......
}
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try{
demo=Class.forName("Reflect.Person");
}catch (Exception e) {
e.printStackTrace();
}
//取得父類
Class<?> temp=demo.getSuperclass();
System.out.println("繼承的父類為: "+temp.getName());
}
}
【運(yùn)行結(jié)果】:
繼承的父類為: java.lang.Object
7. 獲得其他類中的全部構(gòu)造函數(shù)
package Reflect;
class Demo{
//other codes...
}
class Person implements China{
......
}
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try{
demo=Class.forName("Reflect.Person");
}catch (Exception e) {
e.printStackTrace();
}
Constructor<?>cons[]=demo.getConstructors();
for (int i = 0; i < cons.length; i++) {
System.out.println("構(gòu)造方法: "+cons[i]);
}
}
}
【運(yùn)行結(jié)果】:
構(gòu)造方法: public Reflect.Person()
構(gòu)造方法: public Reflect.Person(java.lang.String)
8. 取得其他類的全部屬性
package Reflect;
class Demo{
//other codes...
}
class Person implements China{
......
}
class hello {
public static void main(String[] args) {
Class<?> demo = null;
try {
demo = Class.forName("Reflect.Person");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("===============本類屬性========================");
// 取得本類的全部屬性
Field[] field = demo.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
// 權(quán)限修飾符
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);
// 屬性類型
Class<?> type = field[i].getType();
System.out.println(priv + " " + type.getName() + " "
+ field[i].getName() + ";");
}
System.out.println("===============實(shí)現(xiàn)的接口或者父類的屬性========================");
// 取得實(shí)現(xiàn)的接口或者父類的屬性
Field[] filed1 = demo.getFields();
for (int j = 0; j < filed1.length; j++) {
// 權(quán)限修飾符
int mo = filed1[j].getModifiers();
String priv = Modifier.toString(mo);
// 屬性類型
Class<?> type = filed1[j].getType();
System.out.println(priv + " " + type.getName() + " "
+ filed1[j].getName() + ";");
}
}
}
【運(yùn)行結(jié)果】:
===============本類屬性========================
private java.lang.String sex;
===============實(shí)現(xiàn)的接口或者父類的屬性========================
public static final java.lang.String name;
public static final int age;
9. 通過反射調(diào)用其他類中的方法
package Reflect;
class Demo{
//other codes...
}
class Person implements China{
......
}
class hello {
public static void main(String[] args) {
Class<?> demo = null;
try {
demo = Class.forName("Reflect.Person");
} catch (Exception e) {
e.printStackTrace();
}
try{
//調(diào)用Person類中的sayChina方法
Method method=demo.getMethod("sayChina");
method.invoke(demo.newInstance());
//調(diào)用Person的sayHello方法
method=demo.getMethod("sayHello", String.class,int.class);
method.invoke(demo.newInstance(),"Rollen",20);
}catch (Exception e) {
e.printStackTrace();
}
}
}
【運(yùn)行結(jié)果】:
hello ,china
Rollen 20
10. 調(diào)用其他類的 set 和 get 方法
package Reflect;
class Demo{
//other codes...
}
class Person implements China{
......
}
class hello {
public static void main(String[] args) {
Class<?> demo = null;
Object obj=null;
try {
demo = Class.forName("Reflect.Person");
} catch (Exception e) {
e.printStackTrace();
}
try{
obj=demo.newInstance();
}catch (Exception e) {
e.printStackTrace();
}
setter(obj,"Sex","男",String.class);
getter(obj,"Sex");
}
/**
* @param obj
* 操作的對象
* @param att
* 操作的屬性
* */
public static void getter(Object obj, String att) {
try {
Method method = obj.getClass().getMethod("get" + att);
System.out.println(method.invoke(obj));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param obj
* 操作的對象
* @param att
* 操作的屬性
* @param value
* 設(shè)置的值
* @param type
* 參數(shù)的屬性
* */
public static void setter(Object obj, String att, Object value,
Class<?> type) {
try {
Method method = obj.getClass().getMethod("set" + att, type);
method.invoke(obj, value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
【運(yùn)行結(jié)果】:
男
——更多——
參考:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html