<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    samtiger


    QQ:418148757
    Name:yangchuan
    數(shù)據(jù)加載中……
    java反射基礎(chǔ)
            每一個(gè)java類在內(nèi)存中都對應(yīng)著有一個(gè)Class類對象,這個(gè)對象記錄著該java類的相關(guān)信息。我們可以通過這個(gè)Class對象去:
                a: 在運(yùn)行時(shí)判斷任意一個(gè)對象所屬的類;
                 b:在運(yùn)行時(shí)構(gòu)造任意一個(gè)類的對象。
                 c:在運(yùn)行時(shí)獲取任意一個(gè)類所具有的成員變量和方法。
                 d: 在運(yùn)行時(shí)調(diào)用任意一個(gè)對象的可對外訪問的方法;
                 e:在運(yùn)行時(shí)給任意一個(gè)對象的可對外訪問的成員變量賦值;
      心情不是很好,現(xiàn)發(fā)點(diǎn)代碼。明天來修改吧,唉,居然為了女人心情不好。郁悶阿
    /*
     * @(#)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 閱讀(338) 評論(1)  編輯  收藏

    評論

    # re: java反射基礎(chǔ) 2007-11-15 14:34 程佳

    多發(fā)些東西來喲
    我們好學(xué)習(xí)下撒
      回復(fù)  更多評論    

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲男人的天堂www| 91视频国产免费| 永久免费A∨片在线观看| 一个人看的www在线免费视频| 羞羞网站免费观看| 曰批免费视频播放免费| 深夜福利在线免费观看| 免费精品国自产拍在线播放| 国产成人不卡亚洲精品91| 免费人成网站永久| 国产特黄特色的大片观看免费视频| 成人一区二区免费视频| 国产成人AV免费观看| 国产精品免费一区二区三区四区| 免费av一区二区三区| 91久久精品国产免费一区| 久久久久久免费视频| 在线成人a毛片免费播放| 国产jizzjizz免费看jizz| 亚洲精品成人片在线观看| 不卡一卡二卡三亚洲| 亚洲狠狠婷婷综合久久久久| 亚洲好看的理论片电影| 亚洲电影唐人社一区二区| 亚洲熟女综合色一区二区三区| 国产偷国产偷亚洲清高APP| 一级一级毛片免费播放| 成人爽a毛片免费| 久久精品国产免费观看| 国产精品免费视频网站| 久久青青草原亚洲av无码| 亚洲av日韩av不卡在线观看| 亚洲jjzzjjzz在线播放| 日韩在线视精品在亚洲| CAOPORM国产精品视频免费| 18禁美女裸体免费网站| 黄网址在线永久免费观看 | 免费观看四虎精品成人| 久久久久国产免费| 女人让男人免费桶爽30分钟| 亚洲欧洲日本在线|