小编lea*_*ori的帖子

如何通过“双”对象获取“double.class”

描述:

  • 我需要使用getMethod,它需要参数类型。
  • origin 方法需要double(原始类型,而不是Double),并且我无法更改 origin 方法。
  • 我不能只输入double.class参数s类型,因为可能不同的类型,例如Integer(not int)。
  • 中的方法参数Foo.java始终且仅是原始类型。

代码:

test.java
    public static void main( String args[] )
    {
        Object obj = new Foo();
        Object s = 1.2;
        String type = "Double";
        try {
            Method method = obj.getClass().getMethod("return" + type, s.getClass());// got NoSuchMethodException here, because it requires `double` not Double
            System.out.println(method.invoke(obj,s));
        } catch (NoSuchMethodException | IllegalAccessException |InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
Foo.java //(I …
Run Code Online (Sandbox Code Playgroud)

java reflection

4
推荐指数
1
解决办法
75
查看次数

标签 统计

java ×1

reflection ×1