Control method called using variables

Zarwan

so I'm trying to combine multiple methods that sort an array based on different parameters for optimization. The entire method is the same except for this line:

for (int k = i; k >= 0 && currData.METHOD() <= playerData[k].METHOD(); k--) {

    //The stuff inside the method is the same
}

Where METHOD is being changed to different accessor methods, to control how the array is being sorted.

I did a little bit of searching and came across Java reflection (I'm not sure what it is though). Could someone explain how I could use that to solve my problem, or another better solution? Thanks.

Shadow

As long as the variables are stored, you can do something like this:

String what = new Scanner(System.in).nextLine();
System.out.println(what.getClass().getMethod(what).invoke(what));

If I enter the word length as my input, the output is 6. Because the code calls the function I enter on the string. They don't have to be the same variable though, you could do something like:

for (int k = i; k >= 0 && currData.getClass().getMethod(methodName).invoke(currData)
<= playerData[k].getClass().getMethod(methodName).invoke(playerData[k]); k--)

You can also pass arguments to the functions, if you need to, it's the second parameter after the function name. Just make your variables equal to the function name you want called, and bob's your uncle, should work like a charm.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Is there a way to access the variables/fetch the assigned values of the caller class in a method of the called class?

분류에서Dev

Calling a Method if another Method is called

분류에서Dev

When is toString() method called

분류에서Dev

Using variables defined in one method inside another in Objective-C

분류에서Dev

How to Pass variables inside functions using new method

분류에서Dev

In Java is there a name for the object the method is called on?

분류에서Dev

AbstractTableModel setValueAt method not being called

분류에서Dev

Should a method called by a constructor be static?

분류에서Dev

Should a method called by a constructor be static?

분류에서Dev

How to stub a method that is called in initialize method

분류에서Dev

Why are static variables and methods in java called directly?

분류에서Dev

Application onCreate method gets called twice

분류에서Dev

Flutter: Geolocator return the method 'compareTo' was called on null

분류에서Dev

why is mockito not called when executing mocked method?

분류에서Dev

NSInvocation & NSTimer - Method gets called twice

분류에서Dev

Broadcast Receiver "on recieve" method not been called?

분류에서Dev

shouldPerformSegueWithIdentifier called before checking other method issue

분류에서Dev

Get called method name without __call

분류에서Dev

Dynamically call a method from a dynamically called class

분류에서Dev

Called method output differs from Caller

분류에서Dev

PHP thread - run method not being called

분류에서Dev

addConstraint method being called on unrelated view object

분류에서Dev

Reentrant Synchronization- Unlocking of called synchronized method

분류에서Dev

What method is called when UINavigationController is presented?

분류에서Dev

php variables and forms method POST

분류에서Dev

Better method to assign values to variables

분류에서Dev

handle different instance variables in method

분류에서Dev

Does new transaction starts when method b() is called from method a()?

분류에서Dev

Change Text of server Control in static Web Method

Related 관련 기사

  1. 1

    Is there a way to access the variables/fetch the assigned values of the caller class in a method of the called class?

  2. 2

    Calling a Method if another Method is called

  3. 3

    When is toString() method called

  4. 4

    Using variables defined in one method inside another in Objective-C

  5. 5

    How to Pass variables inside functions using new method

  6. 6

    In Java is there a name for the object the method is called on?

  7. 7

    AbstractTableModel setValueAt method not being called

  8. 8

    Should a method called by a constructor be static?

  9. 9

    Should a method called by a constructor be static?

  10. 10

    How to stub a method that is called in initialize method

  11. 11

    Why are static variables and methods in java called directly?

  12. 12

    Application onCreate method gets called twice

  13. 13

    Flutter: Geolocator return the method 'compareTo' was called on null

  14. 14

    why is mockito not called when executing mocked method?

  15. 15

    NSInvocation & NSTimer - Method gets called twice

  16. 16

    Broadcast Receiver "on recieve" method not been called?

  17. 17

    shouldPerformSegueWithIdentifier called before checking other method issue

  18. 18

    Get called method name without __call

  19. 19

    Dynamically call a method from a dynamically called class

  20. 20

    Called method output differs from Caller

  21. 21

    PHP thread - run method not being called

  22. 22

    addConstraint method being called on unrelated view object

  23. 23

    Reentrant Synchronization- Unlocking of called synchronized method

  24. 24

    What method is called when UINavigationController is presented?

  25. 25

    php variables and forms method POST

  26. 26

    Better method to assign values to variables

  27. 27

    handle different instance variables in method

  28. 28

    Does new transaction starts when method b() is called from method a()?

  29. 29

    Change Text of server Control in static Web Method

뜨겁다태그

보관