samtiger


QQ:418148757
Name:yangchuan
数据加载中……
java反射基础
        每一个java类在内存中都对应着有一个Class类对象,这个对象记录着该java类的相关信息。我们可以通过这个Class对象去:
            a: 在运行时判断任意一个对象所属的类;
             b:在运行时构造任意一个类的对象。
             c:在运行时获取任意一个类所具有的成员变量和方法。
             d: 在运行时调用任意一个对象的可对外访问的方法;
             e:在运行时给任意一个对象的可对外访问的成员变量赋值;
  心情不是很好,现发点代码。明天来修改吧,唉,居然为了女人心情不好。郁闷阿
/*
 * @(#)UserType.java
 * createTime:2007-10-26 下午03:11:49
 
*/

package com.zdsoft.javaiobase.ref;

/**
 * 
@author sam E-mail:ashan8888@163.com
 * 
@version 1.0
 
*/

public class UserType {
    
private String name;

    
private String sex;

    
private int old;
    
    
public String testName;

    
public String getTestName() {
        
return testName;
    }


    
public void setTestName(String testName) {
        
this.testName = testName;
    }


    
public String getName() {
        
return name;
    }


    
public void setName(String name) {
        
this.name = name;
    }


    
public int getOld() {
        
return old;
    }


    
public void setOld(int old) {
        old 
= old;
    }


    
public String getSex() {
        
return sex;
    }


    
public void setSex(String sex) {
        
this.sex = sex;
    }

}

写一些反射测试代码:
/*
 * @(#)RefTest.java
 * createTime:2007-10-26 下午03:13:24
 
*/

package com.zdsoft.javaiobase.ref;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import junit.framework.TestCase;

/**
 * 
@author sam E-mail:ashan8888@163.com
 * 
@version 1.0
 
*/

public class RefTest extends TestCase {
    
    
private Class cobj;

    @Override
    
protected void setUp() throws Exception {
        cobj 
= Class.forName("com.zdsoft.javaiobase.ref.UserType");
    }


    
public void testCreateObj() throws Exception {
        System.out.println(cobj.newInstance());
    }


    
public void testGetMethods() throws Exception {
        Method[] methods 
= cobj.getDeclaredMethods();
        System.out.println(
"-------Methods------");
        
for (Method m : methods) {
            System.out.println(m.toString());
        }

    }


    
public void testGetMethod() throws Exception {
        Method m 
= cobj.getMethod("setName", String.class);
        System.out.println(
"-------a Method------");
        System.out.println(m.toString());

    }

    
    
public void testGetFields() throws Exception {
        System.out.println(
"------fileds-------");
        Field[] fields 
= cobj.getDeclaredFields();
        
for(Field f : fields){
            System.out.println(f.getName());
        }

    }

    
    
public void testInvoke() throws Exception {
        UserType userType 
= (UserType) cobj.newInstance();
        Method m 
= cobj.getMethod("getName"null);        
        Method ms 
= cobj.getMethod("setName", String.class);
        ms.invoke(userType,
"sam");
        System.out.println(
"------invoke getName-------");
        System.out.println(m.invoke(userType,
null));
        
    }

    
    
public void testFiled() throws Exception {
        Field f
= cobj.getField("testName");
        UserType userType 
= (UserType) cobj.newInstance();
        f.set(userType, 
"sam");
        Method m 
= cobj.getMethod("getTestName"null);        
        System.out.println(m.invoke(userType, 
null));
    }

}


            

posted on 2007-10-26 22:40 sam.chuan.yang 阅读(331) 评论(1)  编辑  收藏

评论

# re: java反射基础 2007-11-15 14:34 程佳

多发些东西来哟
我们好学习下撒
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: