简体   繁体   中英

Create subclass from superclass static main

I have a generic, abstract class ( SuperClass ). I want to have there a main method, that would be a default main for each subclass and would do the same, but with proper subclass object that derived and called it.

Like this:

 public abstract class SuperClass {

    // some code here...

    public static void main(String args[]) {
       // here instantiate the subclass
       // extending this SuperClass, and call
       // some methods
    }
 }

 public SubClass extends SuperClass {
      // here just implement some 
      // abstract methods from SupeClass
      // and NOT implement main()
 }

And now I would like to be able to run the SubClass as standalone program, that executes the default main derived from the SuperClass . How to instantiate the proper SubClass object in the main method?

In C++, AFAIR, there is something like virtual modifier for a method, that I guess would be useful here. How to do in in Java?

如果希望子类成为应用程序入口点,则不继承静态方法,在子类中编写main方法。

You could use Spring IOC for example.

Create an xml file like the following and put in your classpath:

appconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean id="someBean" class="com.company.SubClass"/>

</beans>

Then in your main code you could do something like this:

   public static void main(String args[]) {
       ApplicationContext context = ClassPathXmlApplicationContext("appconfig.xml");
       SuperClass startClass = (SuperClass) context.getBean("someBean");
       startClass.someMethod();
   }

Then your SuperClass will not know about its subclasses (but will know about Spring instead...).

You will have to add some Spring jar files to your classpath as well.

您不能在子类中继承静态方法,但如果您想在c ++中创建类似虚拟的方法,请将您的方法设为抽象或受保护

If by I would like to be able to run the SubClass as standalone program you mean that you want to be able to run something like java my.app.SubClass , that doesn't work, because as everyone already pointed out, static methods are not inherited.

Depending on why you want this strange subclass nesting, you would find a workaround by implementing a non-static main like this:

public class SuperClass{ 
  public static void main(String[] args) {
     SuperClass c = //figure out which class to load via a factor or something
     c.nonStaticMain(args);
  }
  protected void nonStaticMain(String[] args) {
    //do everything from your old main() here
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM