java 如何将 getMethod() 与原始类型一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5032833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:07:45  来源:igfitidea点击:

How to use getMethod() with primitive types?

javareflection

提问by yegor256

This is the class:

这是课程:

class Foo {
  public void bar(int a, Object b) {
  }
}

Now I'm trying to get "reflect" this method from the class:

现在我试图从类中“反射”这个方法:

Class c = Foo.class;
Class[] types = { ... }; // what should be here?
Method m = c.getMethod("bar", types);

回答by BalusC

There's just an int.class.

只有一个int.class.

Class[] types = { int.class, Object.class };

An alternative is Integer.TYPE.

另一种选择是Integer.TYPE

Class[] types = { Integer.TYPE, Object.class };

The same applies on other primitives.

这同样适用于其他原语。

回答by paulturnip

The parameter of the method is a primitive shortnot an Object Short.

该方法的参数是一个原语short而不是一个 Object Short

Reflection will not find the method because you specified an object short. The parameters in getMethod()have to match exactly.

反射将找不到该方法,因为您指定了一个短对象。中的参数getMethod()必须完全匹配。

EDIT: The question was changed. Initially, the question was to find a method that takes a single primitive short.

编辑:问题已更改。最初,问题是要找到一种方法,该方法采用单个原语短路。