Java反射机制实验

Person类

public class Person {
    private String name;
    private String sex;
    private int age;
    private String address;
    private String phone;

    public Person(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

测试


public class Main {
    public static void main(String[] args) throws Exception {
        //获取Class对象的两种基本方法:
        Class personClass1 = Person.class;                      //第一种
        Class personClass2 = Class.forName("test.Person");      //第二种

        // 获取所有共有构造方法:
        Constructor cons[] = personClass1.getConstructors();
        System.out.println(cons[0].getName());
        //用构造方法实例化对象
        Person person = (Person) cons[0].newInstance("小明","男",22);

        Field name = person.getClass().getDeclaredField("name");
        Field sex = person.getClass().getDeclaredField("sex");
        Field age = person.getClass().getDeclaredField("age");

        System.out.println(name);
        System.out.println(sex);
        System.out.println(age);
        //设置似有属性可访问
        name.setAccessible(true);
        System.out.println(name.get(person));
        //实例化类中的方法对象
        Method toStr =  person.getClass().getMethod("toString");
        System.out.println(toStr.invoke(person));

        //利用配置文件读取方法名,和参数
        Properties properties = new Properties();
        InputStream inputStream = Thread.currentThread()
                .getContextClassLoader()
                .getResourceAsStream("test/config.properties");
        try {
            if (inputStream == null) {
                System.out.println("null input stream");
            } else {
                properties.load(new InputStreamReader
                        (inputStream, StandardCharsets.UTF_8));
            }
        } catch (IOException e) {
            System.out.println("读取properties文件出错!");
            e.printStackTrace();
        }

        // 从properties文件中读取相应的属性
        String sMethod = properties.getProperty("method");
        String sArg = properties.getProperty("args");

        Method setMsg =  person.getClass().getMethod(sMethod, String.class);

        setMsg.invoke(person,"15551658783");
        System.out.println(person.toString());

    }
}

配置文件内容

method=setPhone
args="1234567890"
posted @ 2020-04-26 20:27  笑等茶凉  阅读(231)  评论(0编辑  收藏  举报