java.lang.NoSuchMethodException

java.lang.NoSuchMethodException: com.gen.entity.User.sayHi()private void com.gen.entity.User.sayHi(java.lang.String)

at java.lang.Class.getDeclaredMethod(Class.java:2130)
at com.gen.test.TestRefelect1.demo01(TestRefelect1.java:27)
at com.gen.test.TestRefelect1.main(TestRefelect1.java:12)

我的代码是这样的:

package com.gen.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class TestRefelect1 {

	public static void main(String[] args) {
		demo01();
	}

	static void demo01() {
		Properties properties = new Properties();
		String name = "D:\\eclipse-workspace\\WEB\\javaPromote\\src\\com\\gen\\test\\class.txt";
		try {
			properties.load(new FileInputStream(name));
			String classname = properties.getProperty("classname");
			String methodname = properties.getProperty("methodname");
			Class<?> forName = Class.forName(classname);
			Method[] declaredMethods = forName.getDeclaredMethods();
			for (Method method : declaredMethods) {
				System.out.println(method);
			}
			Method method = forName.getDeclaredMethod(methodname);
			System.out.println(method);
			method.setAccessible(true);
			method.invoke(forName, "lisi");
		} catch (IOException | ClassNotFoundException | SecurityException
				| IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

错误

在这里插入图片描述
所有的方法可以遍历出来,但是调用特定的方法却不可以

改正
package com.gen.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class TestRefelect1 {

	public static void main(String[] args) {
		demo01();
	}

	static void demo01() {
		Properties properties = new Properties();
		String name = "D:\\eclipse-workspace\\WEB\\javaPromote\\src\\com\\gen\\test\\class.txt";
		try {
			properties.load(new FileInputStream(name));
			String classname = properties.getProperty("classname");
			String methodname = properties.getProperty("methodname");
			Class<?> forName = Class.forName(classname);
			Method[] declaredMethods = forName.getDeclaredMethods();
			for (Method method : declaredMethods) {
				System.out.println(method);
			}
			Method method = forName.getDeclaredMethod(methodname,String.class);
			System.out.println(method);
			method.setAccessible(true);
			method.invoke(forName, "lisi");
		} catch (IOException | ClassNotFoundException | SecurityException
				| IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

主要在27行改正了
Method method = forName.getDeclaredMethod(methodname,String.class);

结果

在这里插入图片描述

需要注意的地方

Method Class.getDeclaredMethod(String name, Class<?>... parameterTypes)的作用是获得对象的所有方法

该方法的第一个参数name是要获得方法的名字,第二个参数parameterTypes是按声明顺序标识该方法形参类型,一定要写上(部分教程说这个参数可以省略)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值