JAVA高级应用:反射主要方法详解

1.有构造方法bean累

Demo.java

package com.bean;

public class Demo {
    private int id;
    private String name;
    private String sex;

/*set,get方法*/
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

//构造方法
    public Demo(int id,String name,String sex){
        this.id = id;
        this.name = name;
        this.sex = sex;
    }
}

2.无构造方法bean类

DemoC.java

package com.bean;

public class DemoC {
    private int id;
    private String name;
    private String sex;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
3.具体实现方法

package com.implement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import com.bean.Demo;
import com.bean.DemoC;

public class RefDemo {
    public static void main(String[] args) throws Exception{
        //setObject();
        //getModifyC();
        //getAllMessage();
        InvokeMethod();
    }
    //获取简单信息
    private static void getSimpleMessage(){
        Demo demo = new Demo(1,"2","3");
        Class d1 = demo.getClass();
        System.out.println("声明Class对象:"+d1);
        System.out.println("获取对象名加路径:"+d1.getName());
        System.out.println("仅获取对象名:"+d1.getSimpleName());
    }
   
    //实例化无构造对象
    private static void setObject() throws Exception{
        DemoC demo = null;
        Class<?>c = null;
        c = Class.forName("com.bean.DemoC");
        demo = (DemoC)c.newInstance();
        demo.setId(1);
        demo.setName("2");
        demo.setSex("3");
        System.out.println(demo.getId());
    }
    //通过构造方法实例化对象
    private static void setObjectConstruct() throws Exception{
        Demo demo = null;
        Class<?> c = null;
        c = Class.forName("com.bean.Demo");
        Constructor con[] = c.getConstructors();
        for (int i = 0; i < con.length; i++) {
            demo = (Demo)con[i].newInstance();
            demo.setId(i);
            System.out.println(demo.getId());
            System.out.println("构造方法:"+con[i]);
        }
    }
    //通过构造方法获取参数修饰符
    private static void getModifyC() throws Exception{
        Demo demo = null;
        Class<?> c = null;
        c = Class.forName("com.bean.Demo");
        Constructor con[] = c.getConstructors();
        for (int i = 0; i < con.length; i++) {
            int mod = con[i].getModifiers();
            System.out.println("构造方法修饰符为:"+Modifier.toString(mod));
            System.out.println("构造方法为:"+con[i].getName());
           
            Class<?> p[] = con[i].getParameterTypes();
            for (int j = 0; j < p.length; j++) {
                System.out.println("构造方法参数为:"+p[j].getName());
                System.out.println("构造方法简单名称为:"+p[j].getSimpleName());
            }
        }
    }
    @SuppressWarnings("unused")
    private static void getAllMessage(){
        DemoC demo = new DemoC();
        Class<?> c = demo.getClass();
        //获取变量信息
        Field field[] = c.getDeclaredFields();
        for (int i = 0; i < field.length; i++) {
            //权限修饰
            String mod = Modifier.toString(field[i].getModifiers());
            //属性类型
            String type = field[i].getType().getName();
            //方法名
            String name = field[i].getName();
            System.out.println(mod+"    "+type+"    "+name);
        }   
    }
   
   
    //常用方法,实体盲比较
    private static void InvokeMethod() throws Exception{
        Demo demo1 = new Demo(1, "2", "3");
        Demo demo2 = new Demo(1, "2", "4");
        Class<?> c1 = demo1.getClass();
        Class<?> c2 = demo2.getClass();
       
        Method method1[] = c1.getMethods();  //获取方法对象
       
        for (int i = 0; i < method1.length; i++) {  //遍历方法对象
            String name1 = method1[i].getName();   //获取方法
            if(name1.contains("get")&&!name1.equals("getClass")){  //获取get方法
               
                Method met1 = c1.getMethod(name1);     //获取具体的方法
                Method met2 = c2.getMethod(name1);
               
                Object obj1 = met1.invoke(demo1);    //获取方法的实现对象
                Object obj2 = met2.invoke(demo2);
               
                if(obj1.equals(obj2)==false){    //若返回值不相等
                   
                    Class<?> type =  method1[i].getReturnType();   //获取方法的返回值类型
                    String setName = "set"+name1.substring("get".length(),name1.length());
                    String setValue = "<font color=red>"+obj2+"</font>";
                    Method method = c2.getMethod(setName, type);  //声明新的方法
                    method.invoke(demo2, setValue);  //调用方法
                }
            }
        }
        System.out.println(demo2.getSex());
    }
}

你可能感兴趣的:(JAVA高级应用:反射主要方法详解)