vkidv
07-29-2007, 11:39 PM
Hi.
I want to loop one of 5 functions in a say, 100 times. I don't want to repeat the loop 5 times for each function - in the function code.
(These numbers are just theoretical)
I believe I can use overriding & extending a class.
Is there a cleaner way to do it?
In javascript, I would do:
function DoThat(What) {
for (var i=0; i < 100;i++)
What() ;
}
Unfortunately you cannot pass functions as references in Java.
In Java, I believe something like this could work:
public class Looper {
public void execute() {
for (int i = 0 ; i < 100 ; i++) {
doThing() ;
}
}
public void what() { }
}
// one of those 5 functions
public class SomeOther() extends Looper{
public void what() {
... // do stuff
}
}
public void main() {
SomeOther my = new SomeOther() ;
SomeOther.execute() ;
}
Is there a simpler way?
I want to loop one of 5 functions in a say, 100 times. I don't want to repeat the loop 5 times for each function - in the function code.
(These numbers are just theoretical)
I believe I can use overriding & extending a class.
Is there a cleaner way to do it?
In javascript, I would do:
function DoThat(What) {
for (var i=0; i < 100;i++)
What() ;
}
Unfortunately you cannot pass functions as references in Java.
In Java, I believe something like this could work:
public class Looper {
public void execute() {
for (int i = 0 ; i < 100 ; i++) {
doThing() ;
}
}
public void what() { }
}
// one of those 5 functions
public class SomeOther() extends Looper{
public void what() {
... // do stuff
}
}
public void main() {
SomeOther my = new SomeOther() ;
SomeOther.execute() ;
}
Is there a simpler way?