Results 1 to 3 of 3
Thread: Function as argument
- 06-05-2010, 07:18 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
Function as argument
Hello, basically, what I need to do, lets say I have 3 functions:
But let's say, that I need to make it so that every time one of these functions is called, a message System.out.println("One of three function has been called");Java Code:String countNumberOfWords() {... return ...} int countDifferential() {... return ...} boolean isPartial() {... return ...}
But I don't want to insert that message into implementation of these 3 functions. I want to have a function, lets call it Announcer, which will take as argument one of these 3 functions, and before returning corresponding values(String/int/boolean) will print out that message.
Or is different approach more efficent ? If yes, how would you do it ? If no, how would you implement my solution ?
I've tried googling this, but none of results were good for me.
Thanks for your time.
- 06-05-2010, 07:45 PM #2
I guess you're a C programmer: functions and passing functions as arguments.
You can pass objects to methods and the object can have callable method.
The objects can implement an interface defining the method to be called.
Otherwise you'll have to add println() statements to each method you've listed to say "I was called with ..."
- 06-05-2010, 08:33 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
Yes I come from heavy c++ background.
Thanks though, problem is solved now.
Truly what I needed to do was to make method which would do some checkings before creating method, so in the end it's like this:
.Java Code:public interface CheckCallInt<T> { T execute(); }
.
.
.Java Code:public static <T> T CheckCall(CheckCallInt<T> Call) { . . lots of code to check few things . return Call.execute(); }
.
.
then I just input Call1234 into CheckCall, and it first check the method and then executes it.Java Code:CheckCallInt Call1234 = new CheckCallInt<String>() { public String execute() { . . do what this method is supposed to do . };
Similar Threads
-
Command line argument
By denisatandi in forum New To JavaReplies: 8Last Post: 10-16-2012, 11:37 PM -
Function as argument?
By martypapa in forum AWT / SwingReplies: 2Last Post: 02-07-2010, 10:02 AM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
Method as an argument?
By StokedOnMe in forum New To JavaReplies: 13Last Post: 09-18-2009, 06:29 AM -
Parsing Argument Values
By vipvan2000 in forum Advanced JavaReplies: 1Last Post: 02-17-2008, 01:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks