Passing a method as a parameter
Author |
Message |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Mon Nov 04, 2013 1:07 pm Post subject: Passing a method as a parameter |
|
|
I was wondering if it was possible to set a parameter or variable as a method. In turing, for instance, you can do this:
Turing: |
var thing : proc x
proc hi
put "hi"
end hi
thing := hi
|
Or in Python there's a method called reduce whose first argument takes in a function. Is there anything like that in java? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Mon Nov 04, 2013 1:58 pm Post subject: RE:Passing a method as a parameter |
|
|
Oh I just discovered you can use "implement" to do sort of the same thing, you define a bunch of classes with one method that is the function you want.
Any other ideas out there? |
|
|
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Mon Nov 04, 2013 2:57 pm Post subject: Re: Passing a method as a parameter |
|
|
Right now you can use an anonymous inner class. Once Java joins the future, you can also use lambda functions like in Python. |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Mon Nov 04, 2013 3:51 pm Post subject: RE:Passing a method as a parameter |
|
|
Well technically speaking I'm not using Java, but something based off of Java (Processing, in case you're curious), so what I stated above works in it but I don't think I can do that |
|
|
|
|
![](images/spacer.gif) |
|
|