in Education by
I have a method that's about ten lines of code. I want to create more methods that do exactly the same thing, except for a small calculation that's going to change one line of code. This is a perfect application for passing in a function pointer to replace that one line, but Java doesn't have function pointers. What's my best alternative? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Anonymous inner class Say you want to have a function passed in with a String param that returns an int. First you have to define an interface with the function as its only member, if you can't reuse an existing one. interface StringFunction { int func(String param); } A method that takes the pointer would just accept StringFunction instance like so: public void takingMethod(StringFunction sf) { int i = sf.func("my string"); // do whatever ... } And would be called like so: ref.takingMethod(new StringFunction() { public int func(String param) { // body } }); EDIT: In Java 8, you could call it with a lambda expression: ref.takingMethod(param -> bodyExpression);

Related questions

0 votes
    I have a method that's about ten lines of code. I want to create more methods that do exactly ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I have a method that's about ten lines of code. I want to create more methods that do exactly ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I have a method that's about ten lines of code. I want to create more methods that do exactly ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    The coalesce function provides one way to substitute constant values for which values Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    What is the substitute of Rhino javascript engine in Java 8? (a) Nashorn (b) V8 (c) Inscript (d) ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    What is the substitute of Rhino javascript engine in Java 8? (a) Nashorn (b) V8 (c) Inscript (d ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
+1 vote
    What is a pointer to a function? Give the general syntax for the same in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    I need to compress portions of our application's network traffic for performance. I presume this means I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I need to compress portions of our application's network traffic for performance. I presume this means I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I need to compress portions of our application's network traffic for performance. I presume this means I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    In nearest neighbour case, the stored pattern closest to input pattern is recalled, where does it occurs? (a) ... d) none of the mentioned Please answer the above question....
asked Sep 5, 2022 in Education by JackTerrance
0 votes
    In Excel, function rounds number down, toward zero, to the nearest multiple of significance. Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    A sum of money is rounded off to the nearest rupee, find the probability that the round off error is at least ten ... `(19)/(101)` Select the correct answer from above options...
asked Nov 16, 2021 in Education by JackTerrance
0 votes
    Is it possible to substitute "ls" command in the place of "echo" command?...
asked May 28, 2021 in Technology by JackTerrance
...