java反射,从配置文件中获取类名、方法名、变量、变量类型并进行方法调用

这篇博客介绍了如何使用Java反射技术,结合配置文件(config.properties)来获取类名、方法名、变量和变量类型,并进行方法调用。通过解决配置文件读取中文乱码问题,将字符串参数转换为Class类型,以及将字符串参数转换为相应数据类型的Object数组,最终成功调用方法。
摘要由CSDN通过智能技术生成
  1. 给定类和配置文件,利用反射调用方法。
    以一个Point类和一个简单的配置文件为例。
    Point类和配置文件config.properties:
	package com.part3.reflect.point;

/**
 * Author: Sean
 * Date: Created In 21:34 2019/4/15
 * Title:
 * Description:
 * Version: 0.1
 * Update History:
 * [Date][Version][Author] What has been done;
 */

public class Point {
    private int x;
    private int y;
    private String name;

    /**
     * 无参构造函数
     */
    public Point() {

    }

    /**
     * 带x的构造函数
     *
     * @param x 横坐标
     */
    public Point(int x) {
        this.x = x;
    }

    /**
     * 带x和y的构造函数
     *
     * @param x 横坐标
     * @param y 纵坐标
     */
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    /**
     * 带所有参数的构造函数
     *
     * @param x    横坐标
     * @param y    纵坐标
     * @param name 名称
     */
    public Point(int x, int y, String name) {
        this.x = x;
        this.y = y;
        this.name = name;
    }

    /**
     * 移动点
     *
     * @param dx   水平偏移分量
     * @param dy   垂直偏移分量
     * @param name 新的名称
     */
    public void move(int dx, int dy, String name) {
        x += dx;
        y += dy;
        this.name = name;
    }

    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + 31;
        result = prime * result + 31;
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值