Class Object的使用

[code]
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;

public class TestRef {

public static void main(String[] args) throws Exception {
TestRef testRef = new TestRef();
Class clazz = TestRef. class ;
System.out.println( " getPackage() = " + clazz.getPackage().getName());
// getModifiers()的返回值可以包含类的种类信息。比如是否为public,abstract,static
int mod = clazz.getModifiers();
System.out.println( " Modifier.isAbstract(mod) = " + Modifier.isAbstract(mod));
System.out.println( " getName() = " + clazz.getName());
System.out.println( " getSuperclass() = " + clazz.getSuperclass().getName());
System.out.println( " getInterfaces() = " + clazz.getInterfaces()); // 实现了哪些Interface
System.out.println( " clazz.getDeclaredClasses() = " + clazz.getDeclaredClasses()); // 包含哪些内部类
System.out.println( " getDeclaringClass() = " + clazz.getDeclaringClass()); // 如果clazz是inner class 那么返回其outer class

System.out.println( " ---------- " );
Constructor[] constructor = clazz.getDeclaredConstructors(); // 返回一组构造函数 Constructor[]
if (constructor != null ) {
for ( int i = 0 ; i < constructor.length; i ++ ) {
System.out.println(constructor[i].getName());
}
}

System.out.println( " ---------- " );
Method[] method = clazz.getDeclaredMethods(); // Method[]
if (method != null ) {
for ( int i = 0 ; i < method.length; i ++ ) {
System.out.println(method[i].getName());
}
}

System.out.println( " ---------- " );
Field[] field = clazz.getDeclaredFields(); // Field[]
if (field != null ) {
for ( int i = 0 ; i < field.length; i ++ ) {
System.out.println(field[i].getName());
System.out.println(field[i].getType().getName());
System.out.println(field[i].get(testRef));
}
}

// 动态生成instance(无参数)
Class clz = Class.forName( " reflection.TestRef " );
Object obj = clz.newInstance();
System.out.println(((TestRef)obj).getStr());

// 动态生成instance(有参数)
Class[] params = new Class[] {String. class , int . class , double . class } ;
Constructor construct = clz.getConstructor(params);
// JDK1.5的情况下可以直接用{"haha",999,100.01}作为参数
Object obj2 = construct.newInstance( new Object[] { " haha " , new Integer( 999 ), new Double( 100.01 )} );
System.out.println(((TestRef)obj2).getStr());

// 动态调用method(public method)
Class[] params2 = new Class[] {String. class } ;
Method methods = clz.getMethod( " setStr " , params2);
methods.invoke(testRef, new Object[] { " invoke method " } );
System.out.println(testRef.getStr());

// 动态改变field内容(public field)
Field fields = clz.getField( " str " );
fields.set(testRef, " set field's value " );
System.out.println(testRef.getStr());

}

public TestRef() {
System.out.println( " --- complete TestRef() --- " );
}

public TestRef(String str, int i, double d) {
this .str = str;
this .i = i;
this .d = d;
System.out.println( " --- complete TestRef(String str, int i, double d) --- " );
}

public String str = " I'm a string " ;

int i = 1 ;

double d = 3.14 ;

HashMap map = new HashMap();

public double getD() {
return d;
}

public void setD( double d) {
this .d = d;
}

public int getI() {
return i;
}

public void setI( int i) {
this .i = i;
}

public HashMap getMap() {
return map;
}

public void setMap(HashMap map) {
this .map = map;
}

public String getStr() {
return str;
}

public void setStr(String str) {
this .str = str;
}
} [/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值