CSDN博客

img doc123

JAVA反射机制实例代码

发表于2008/9/19 11:14:00  1005人阅读

 
首先在工程中建立 一个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());
    }
}

 

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

0 0

相关博文

我的热门文章

img
取 消
img