反射案例。

1.修改配置文件方式

建立两个JavaBean类

package com.heima.itcast.domain;

/**
 * @author : Rain
 * @version : 1.0
 * @className : Person
 * @description : Person类
 * @createTime : 2022/10/12 21:28
 */
public class Person {
    private String name;
    private int age;
    public String a;
    protected String b;
    String c;
    private String d;

    public Person() {
    }

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

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     *
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", a='" + a + '\'' +
                ", b='" + b + '\'' +
                ", c='" + c + '\'' +
                ", d='" + d + '\'' +
                '}';
    }

    public void eat() {
        System.out.println("吃...");
    }

    public void eat(String food) {
        System.out.println("吃..." + food);
    }
}

package com.heima.itcast.domain;

/**
 * @author : Rain
 * @version : 1.0
 * @className : Student
 * @description : Student类
 * @createTime : 2022/10/14 11:04
 */
public class Student {
    public void sleep() {
        System.out.println("sleep...");
    }
}

在src下创建一个配置文件,文件里面写

//第一类的路径,第二个写类的方法
//后面通过修改这两个参数来实现创建任意类的方法,执行任意方法
className=com.heima.itcast.domain.Person
methodName=eat

反射:

package com.heima.itcast.reflect;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @author : Rain
 * @version : 1.0
 * @className : ReflectTest0
 * @description : 框架类
 * @createTime : 2022/10/14 11:04
 */
public class ReflectTest0 {
    public static void main(String[] args) throws Exception {
        //1.加载配置文件
        //1.1创建Properties对象
        Properties pro = new Properties();
        //1.2加载配置文件,转换为一个集合
        //1.2.1获取class目录下的配置文件路径
        ClassLoader classLoader = ReflectTest0.class.getClassLoader();
        InputStream is = classLoader.getResourceAsStream("pro.properties");
        pro.load(is);

        //2.获取配置文件中定义的数据
        String className = pro.getProperty("className");
        String methodName = pro.getProperty("methodName");

        //3.加载该类进内存
        Class cls = Class.forName(className);
        //4.创建对象
        Object obj = cls.newInstance();
        //5.获取方法对象
        Method method = cls.getMethod(methodName);
        //6.执行方法
        method.invoke(obj);
    }
}

2.注解方式

package com.heima.itcast.reflect;

/**
 * @author : Rain
 * @version : 1.0
 * @className : Demo2
 * @description : 描述说明该类的功能
 * @createTime : 2022/10/14 17:19
 */
public class Demo2 {
    public void show2(){
        System.out.println("demo2...show2...");
    }
}
package com.heima.itcast.reflect;

/**
 * @author : Rain
 * @version : 1.0
 * @className : Demo1
 * @description : 描述说明该类的功能
 * @createTime : 2022/10/14 17:18
 */
public class Demo1 {
    public void show1(){
        System.out.println("demo1...show1...");
    }
}

创建一个注解

package com.heima.itcast.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author : Rain
 * @version : 1.0
 * @className : Pro
 * @description : 描述需要执行的类名,方法名
 * @createTime : 2022/10/14 16:39
 */
@Target(value = ElementType.TYPE)//作用在TYPE(类)上
@Retention(RetentionPolicy.RUNTIME)//保留在RUNTIME阶段
public @interface Pro {
    String className();
    String methodName();
}

package com.heima.itcast.annotation;

import java.lang.reflect.Method;

/**
 * @author : Rain
 * @version : 1.0
 * @className : ReflectTest1
 * @description : 框架类
 * @createTime : 2022/10/14 15:57
 */

//前提:不改变该类的任何代码,可以创建任意类对象,执行任意类方法
@Pro(className = "com.heima.itcast.reflect.Demo2",methodName = "show2")//通过修改此处注解的值,达到相同效果
public class ReflectTest1 {
    public static void main(String[] args) throws Exception {
        //解析注解
        //1.1获取该类的字节码文件对象
        Class<ReflectTest1> reflectTest1Class = ReflectTest1.class;
        //2.获取上边的注解对象
        Pro annotation = reflectTest1Class.getAnnotation(Pro.class);//在内存中去生成一个该注解接口的子类实现对象
        //3.调用注解中定义的抽象方法,获取返回值
        String className = annotation.className();
        String methodName = annotation.methodName();
        System.out.println(className);
        System.out.println(methodName);

        //3.加载该类进内存
        Class cls = Class.forName(className);
        //4.创建对象
        Object obj = cls.newInstance();
        //5.获取方法对象
        Method method = cls.getMethod(methodName);
        //6.执行方法
        method.invoke(obj);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值