Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

java - Java反射-使用int [] []参数调用方法[duplicate](Java reflection - Invoke method with int[][] argument [duplicate])

I have this method defined within MatrixOperation class:

(我在MatrixOperation类中定义了此方法:)

private static int getLargestSubMatrixSize(int[][] inputMatrix, int[][] arrResult){}

I need to invoke this method using reflection

(我需要使用反射来调用此方法)

I tried this approach, but I'm getting a NoSuchMethodException :

(我尝试了这种方法,但是却遇到了NoSuchMethodException :)

Method method = MatrixOperations.class.getMethod("getLargestSubMatrixSize", int[][].class, int[][].class);
method.setAccessible(true);
int maxCount = (int) method.invoke(null, inputMatrix, resultMatrix);

How can this be accomplished?

(如何做到这一点?)

  ask by Curious Coder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your method is private and getMethod only returns public methods:

(您的方法是private方法,而getMethod仅返回公共方法:)

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

(返回一个Method对象,该对象反映此Class对象表示的类或接口的指定公共成员方法。)

What you want to use instead is getDeclaredMethod :

(您要使用的是getDeclaredMethod :)

Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.

(返回一个Method对象,该对象反映此Class对象表示的类或接口的指定声明方法。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...