简体   繁体   English

Java:如何将类型数组类作为参数传递?

[英]Java: How to pass typed-array class as parameter?

I have some 3rd-party API with method someFn(int code, int[] data) . 我有一些使用方法someFn(int code, int[] data)的第三方API。

How can I express types (particulary array-typed argument) outside API container (interface class) for usage in Class.getMethod(name, Integer.class, ???.class); 我如何在API容器(接口类)外部表达类型(尤其是数组类型的参数)以用于Class.getMethod(name, Integer.class, ???.class); ? How can I describe argument of array type properly? 如何正确描述数组类型的参数?

As far as I know, I may use workaround like, but is there a better way of doing this? 据我所知,我可以使用类似的解决方法,但是有更好的方法吗?

class C { void f(int[] a) {} } }

C.class.getDeclaredMethods()[0].getParameterTypes()[0]

Thanks in advance 提前致谢

你可以用int[].class以获取类文字的int []并把它传递给Class.getMethod

Class.getMethod(name, Integer.class, int[].class);
    Class.getMethod(name, Integer.class, int[].class);

int[]是类,因此int[].class是引用int数组类的有效语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在类复制构造函数(Java OOP)中正确传递数组作为参数 - How to properly pass an array as a parameter in a class copy constructor (Java OOP) Java泛型。 如何正确扩展扩展通用抽象参数类型的类的参数类型的类 - Java Generics. How to properly extend parameter typed class that extends Generic abstract parameter typed class 如何在Nativescript中使用接口类型的参数调用Java方法(自定义类) - How to call Java method (custom class) with an interface typed parameter in Nativescript 如何使用也键入的类型参数创建java.lang.Class? - How to create java.lang.Class with type parameter that is also typed? 是否可以使用带类型类参数的java注释? - Are java annotations with typed class parameter possible? 如何传递Class类型的参数 <T[]> java的 - How to pass the parameter of type Class<T[]> java 如何将类的实例传递给作为参数的数组? - How to pass an instance of a class to an array that is a parameter? 如何在java中的石英调度中将数组作为参数传递 - How to pass an array as a parameter in quartz scheduling in java 如何在java方法中将数组作为参数传递? - How to pass array as parameter in java method? 如何将参数从jsp传递给java类? - How to pass a parameter from jsp to java class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM