interface - Java Pass Method as Parameter -


I'm exploring a way to pass the method by reference. I understand that Java does not pass methods in the form of parameters, however, I want to get an option

I have been told that there are options to overcome the methods of interface parameters, But I do not know how an interface can work as a reference by reference. If I understand correctly, then an interface is only a summary of methods that are not defined. I need to define an interface every time because many different methods do not want to send the same parameters as well as the method can say

What is my goal to do this? Something similar for

  public zero setAllComponents (component [] myComponentArray, method MyMethod) {For (Component Leaf: myComponentArray) {(Container instanceof Leaf) If {// recursive call}, Container container node = (Container) leaf; SetAllComponents (node.getComponents (), myMethod); } // end if node myMethod (leaf); } // looping through end components}  

apply as:

  setAllComponents (this.getComponents (), changeColor ()); SetAllComponents (this.getComponents (), changeSize ());  itemprop = "text"> 

: As a Java 8, Good solutions are described as. The following answer was written for Java7 and the first ...


  Take a look at // Note:. Code is not tested, but I believe this is valid Java ... public class CommandExample {public interface command {public zero executed (object data); } Public Square PrintCommand Command {executes public void (object data) {println (data.toString ()); }} Public static zero call command (command command, object data) {command.execute (data); } Public static zero major (string ... args) {callCommand (new print command ("," Hello world "); }}  

Edit: , there is another way to do this. The visitor's approach is a bit more - your nodes need to be Visitor-aware by a acceptance vicitor () method - but if you need to face more complex object graphs Checkable.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -