I want to define a base class that defines the main method that instantiates the class and runs the method. However, there are a couple of problems. Here is the base class:
public abstract class Strategy
{
abstract void execute(SoccerRobot robot);
public static void main(String args)
{
Strategy s = new ();
s.execute(new SoccerRobot())
}
}
And here is an example of a derived class:
public class UselessStrategy
{
void execute(SoccerRobot robot)
{
System.out.println("I'm useless")
}
}
It defines a simple execute method that should be called in the main method when used as the main application. However, for this I need to create an instance of the derived class from the main method of the base class. This is not possible.
I would prefer not to repeat the main method for each derived class, as it feels somewhat unobvious.
Is there a proper way to do this?
source
share