zoukankan      html  css  js  c++  java
  • object is not an instance of declaring class

    错误信息:object is not an instance of declaring class

    说明Class没有实例化;
    解决办法:
    由于没有实力化可以有如下两种方法:
    1、反射方法定义成为static的,故被反射类就不需要实例化;
    2、method.invoke(class.newInstance(), args);

    举栗子:对应第一种方法

    public static void testSys(String msgs) {
    System.out.println("java 反射机制调用方法执行,msg:" + msgs);
    }

    @Test
    public void test(){
    System.out.println("-------------获取当前类-----------------------");
    String msg = "hello";
    Class<? extends Test> aClass1 = getClass();
    Method[] methods = aClass1.getMethods();
    for (Method method : methods) {
    if (method.getName().equals("testSys")) {
    System.out.println("匹配成功,开始执行反射方法,methodName:" + method.getName());
    System.out.println("aClass1:" + aClass1);
    System.out.println("aClass1Name:" + aClass1.getName());
    method.invoke(aClass1, msg);
    System.out.println("执行完成.....");
    }
    }
    }

    举栗子:对应第二种方法

    public void testSys(String msgs) {
    System.out.println("java 反射机制调用方法执行,msg:" + msgs);
    }

    @Test
    public void test(){
    System.out.println("-------------获取当前类-----------------------");
    String msg = "hello";
    Class<? extends Test> aClass1 = getClass();
    Method[] methods = aClass1.getMethods();
    for (Method method : methods) {
    if (method.getName().equals("testSys")) {
    System.out.println("匹配成功,开始执行反射方法,methodName:" + method.getName());
    System.out.println("aClass1:" + aClass1);
    System.out.println("aClass1Name:" + aClass1.getName());
    method.invoke(aClass1.newInstance(), msg);
    System.out.println("执行完成.....");
    }
    }
    }
     
  • 相关阅读:
    记住我
    国米夺冠
    小谈“汉字转换成拼音(不带声调)”
    NLP资源共享盛宴
    文本分类资源和程序开源共享
    菜鸟进阶:C++实现Chisquare 特征词选择算法
    欢迎大家试用信息领域学科知识服务平台
    欢迎大家加入NLP,WEBIR,DATA Ming 的技术QQ群
    求两点之间所有路径的算法
    step by step 文本分类(一)
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/11287470.html
Copyright © 2011-2022 走看看