Create subclass from superclass static main

狂风中的少年 提交于 2020-01-03 19:05:14

问题


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?

  • I cannot do just new because in SuperClass I don't know the actual name of a SubClass
  • I cannot do it using reflection, because I cannot get the name of the SubClass from a static method implemented in the SuperClass (Getting the name of a sub-class from within a super-class)

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?


回答1:


Static methods are not inherited, if you want your subclass to be your application entry point, program the main method in the subclass.




回答2:


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.




回答3:


You can not inherit static method in subclasses but if you want to make a method like virtual in c++ , make your method abstract or protected




回答4:


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
  }
} 


来源:https://stackoverflow.com/questions/11313271/create-subclass-from-superclass-static-main

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