Inheriting the main method

青春壹個敷衍的年華 提交于 2019-12-06 02:45:31

Move the main method out into a separate class. Separate concerns
Strategy (the name says it all)
Launcher (assembling components together and triggering execution)

public class Launcher
{
    public static void main(String args)
    {
       Strategy s = new UselessStrategy();
          //OR Strategy s = CreateInstance(args[0]) ;
          //OR equiv mechanism for Dependency Injection if you don't want to hardcode the derived strategy to use.
        s.execute(new SoccerRobot())
    }
}

Static methods, such as "main", are not inherited but can be called directly. As a workaround, you could parameterize the class name as an argument to the main method:

public static void main(String args) throws Exception
{
  String className = (args.length > 0) ? args[0] : 'UselessStrategy';
  Strategy s = (Strategy) Class.forName(className).newInstance();
  s.execute(new SoccerRobot())
}

If Class.forName is not possible, then maintaining a mapping of class names can provide a lookup table, per Andreas_D's comment:

private static Map<String, Class<? extends Strategy>> STRATEGY_NAME =
    new HashMap<String, Class<? extends Strategy>>();

static {
    STRATEGY_NAME.put("Useless", UselessStrategy.class);
    STRATEGY_NAME.put("Better", BetterStrategy.class);
}

public static void main(String args[]) throws Exception {
    String className = (args.length > 0) ? args[0] : null;
    Class<? extends Strategy> klass = STRATEGY_NAME.get(className);
    if (klass == null) klass = UselessStrategy.class;
    Strategy s = klass.newInstance();
    s.execute();
}

Automated methods for maintaining the mapping could be devised, such as using reflection, if the need arises.

You can define the class in a static block in the subclass.

public abstract class Strategy
{
    protected static Class<? extends Strategy> instanceClass;

    abstract void execute(SoccerRobot robot);

    public static void main(String args)
    {
        Strategy s = instanceClass.newInstance()
        s.execute(new SoccerRobot())
    }
}

and then

public class UselessStrategy extends Strategy
{
    static {
        instanceClass = UselessStrategy.class;
    }

    void execute(SoccerRobot robot)
    {
        System.out.println("I'm useless")
    }
}

You cannot instantiate an abstract class, but you definitely can instantiate a derived class from the base class. So just remove abstract from class definition

public class UselessStrategy

and do

Strategy s = new UselessStrategy();

I'd rethink this.

Put the code that you'd like executed somewhere else, preferably a non-static method, and call that. main() shouldn't be used this way.

I'd recommend creating a separate Strategy class in lieu of main.

Where is the main method called from? If it takes arguments then you can decide a concrete strategy based on those arguments, instantiate that strategy class and call the execute method on it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!