实验一 利用java反射技术来生成一个类的实例,不要用new生成

1. 先准备一个实体对象类

在src目录下新建一个test的包,里面创建一个person.class类,里面有属性为姓名、年龄、电话、身高和体重5个属性

package test;

public class Person {
    private String name;    // 姓名
    private int age;        // 年龄
    private String tel;     // 电话
    private double height;  // 身高
    private double weight;  // 体重

//    自我介绍
    public void introduction(){
        String str = "Hello! My name is "+this.name+". I am "+this.age+" years old. My telephone number is "+tel+". My height is "+this.height+", and my weight is "+this.weight+".Thankyou!";
        System.out.println(str);
    }

//    方便测试用
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", tel='" + tel + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                '}';
    }

//    无参构造方法
    public Person() {
    }

//    有参构造方法
    public Person(String name, int age, String tel, double height, double weight) {
        this.name = name;
        this.age = age;
        this.tel = tel;
        this.height = height;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
}

2. 创建一个测试类

PersonTest.class

package test;

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

public class PersonTest3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
//        1.通过字符串传参来获取反射对象
        Class person = Class.forName("test.Person");
//        2.利用反射对象来生成目标对象
        Person zhangsan = (Person) person.getConstructor(String.class,int.class,String.class,double.class,double.class).newInstance("张三",18,"12345678909",178,70);
        System.out.println(zhangsan);

//        3.获取成员变量
//        getDeclaredField()用于获取私有、受保护或公有的属性都可以
        Field f = person.getDeclaredField("age");
//        暴力破解,将private修饰的属性也可以访问修改
        f.setAccessible(true);
//        将zhangsan的对象中的age属性值设置为20
//        等同于:age=20
        f.set(zhangsan,20);
        System.out.println(zhangsan);

//        4.获取成员方法
//        类似于获取成员变量,getMethod只能用于被public修饰的方法,getDeclaredMethod()用于获取私有、受保护或公有的方法
//        通过setAge()方法来修改age值 获取setAge()方法
//        int.class是setAge()方法中的参数类型
        Method method = person.getMethod("setAge", int.class);
//        invoke: 执行方法,执行(zhangsan)对象中的setAge()方法,里面传参为30
//        等同于  zhangsan.setAge(30)
        method.invoke(zhangsan,30);
        System.out.println(zhangsan);

//        用反射来调用一下自我介绍方法
        Method m1 = person.getMethod("introduction");
        m1.invoke(zhangsan);
    }
}

3. 运行结果

在这里插入图片描述

4. 注意事项

getConstructor() 用于获取构造方法,当获取有参构造方法时需要指定参数的类型,再用newInstance()传输参数实现创建对象,但是newINstance()创建出来的对象为Object类型,需要强转为需要的对象类型。当然,直接用Object对象也是可以获取例子中的person的任意属性与方法的。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值