Chinaunix首页 | 论坛 | 博客
  • 博客访问: 409867
  • 博文数量: 114
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1395
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-05 17:54
文章分类

全部博文(114)

文章存档

2011年(2)

2009年(1)

2008年(111)

我的朋友

分类: Java

2008-07-19 20:34:46

首先在工程中建立 一个Bean类

public class UserBean {
    private String userName ="admin";
    private String passWord = "888";
    public String getPassWord() {
        return passWord;
    }
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public void setUserName(int i) {
        
    }
}

 

然后 新建一个类 main.java

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

public class Main {
    
    //根据字段调用所有的set方法,传入死值 love

    private void dox8() throws Exception {
        // TODO Auto-generated method stub

        
        Class cls = Class.forName("com.accp.UserBean");
        Object o = cls.newInstance();
        
        Field[] fields = cls.getDeclaredFields();
        
        for (Field field : fields) {
            String fieldName = field.getName();
            
            Method method = cls.getDeclaredMethod("set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1),new Class[]{String.class});
            method.invoke(o, new Object[]{"love"});
        }
        
        UserBean bean = (UserBean)o;
        
        System.out.println(bean.getUserName()+" "+bean.getPassWord());
        
    }
    
    private void dox7() throws Exception
    {
        Class cls = Class.forName("com.accp.UserBean");
        Object o = cls.newInstance();
        
        Method m = cls.getDeclaredMethod("setUserName",new Class[]{String.class});
        
        m.invoke(o,new Object[]{"John.Lenoe"});
        
        
        UserBean bean = (UserBean)o;
        
        System.out.println(bean.getUserName());
        
    }
    
    //通过字段找方法

    private void dox6() throws Exception {
        // TODO Auto-generated method stub

        Class cls = Class.forName("com.accp.UserBean");
        Object o = cls.newInstance();
        
        Field field = cls.getDeclaredField("userName");
        
        String fieldName = field.getName();
        
        Method method = cls.getDeclaredMethod("get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1));
        
    }
    
    //调用其中一个已知方法名的方法

    private void dox5() throws Exception {
        // TODO Auto-generated method stub

        Class cls = Class.forName("com.accp.UserBean");
        Object obj = cls.newInstance();
        
        Method method = cls.getDeclaredMethod("getUserName");
        Object o = method.invoke(obj);
        System.out.println(o);
    }
    
    //调用当前类的所有get方法

    private void dox4() throws Exception {
        // TODO Auto-generated method stub

        Class cls = Class.forName("com.accp.UserBean");
        Object obj = cls.newInstance();
        Method[] methods = cls.getDeclaredMethods();
        
        for (Method method : methods) {
            if(method.getName().startsWith("get"))
            {
                Object temp = method.invoke(obj);
                System.out.println(temp);
            }
        }
        
    }
    
    //取出当前类中所有的方法

    private void dox3() throws Exception {
        // TODO Auto-generated method stub

        Class cls = Class.forName("com.accp.UserBean");
        Method[] methods = cls.getDeclaredMethods();
        
        for (Method method : methods) {
            System.out.println(method.getName()+" "+method.getReturnType());
        }
    }
    
    
    //取出当前类中所有的字段

    private void dox2() throws ClassNotFoundException {
        // TODO Auto-generated method stub

        Class cls = Class.forName("com.accp.UserBean");
        
        Field[] fields = cls.getDeclaredFields();
        
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            System.out.println(field.getName()+" "+field.getType());
        }
        
    }
    
    public static void main(String[] args) throws Exception {
        Main ma = new Main();
        ma.dox8();
    }
    
    //根据传入的类名,生成类结构和类实例

    private void dox() throws Exception {
        // TODO Auto-generated method stub

        Class cls = Class.forName("com.accp.UserBean");
        Object obj = cls.newInstance();
        UserBean bean = (UserBean)obj;
        
        bean.setUserName("admin");
        System.out.println(bean.getUserName());
    }
}

 

这就是基本上总结的反射应用

阅读(1407) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~