【问题标题】:Create subclass from superclass static main从超类静态主创建子类
【发布时间】:2012-07-04 00:34:50
【问题描述】:

我有一个通用的抽象类 (SuperClass)。我希望有一个main 方法,这将是每个子类的默认 main 方法,并且会做同样的事情,但使用正确的子类对象派生并调用它。

像这样:

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

现在我希望能够将SubClass 作为独立程序运行,它执行从SuperClass 派生的默认main。如何在main方法中实例化正确的SubClass对象?

在 C++ 中,AFAIR 中有类似 virtual 修饰符的方法,我想这在这里很有用。用Java怎么做?

【问题讨论】:

    标签: java methods static instantiation derived-class


    【解决方案1】:

    如果我希望能够将子类作为独立程序运行,您的意思是您希望能够运行java my.app.SubClass 之类的东西,这是行不通的,因为作为每个人已经指出,静态方法不会被继承。

    根据您想要这种奇怪的子类嵌套的原因,您可以通过实现这样的非静态 main 找到解决方法:

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

    【讨论】:

    • 你知道...所有这些问题都是关于如何“确定要加载哪个类...”
    • ROFL。从这个问题上看不太清楚:) 传递一个论点。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      例如,您可以使用Spring IOC

      创建一个如下所示的 xml 文件并放入您的类路径:

      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>
      

      然后在你的主代码中你可以做这样的事情:

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

      那么你的 SuperClass 将不知道它的子类(但会知道 Spring 代替......)。

      您还必须将一些 Spring jar 文件添加到您的类路径中。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • (这基本上是说你不能做你想做的事。)
        • 嗯......它们是继承的,因为我可以在 SubClass 上调用从 SuperClass 派生的主要方法并且它可以工作。问题是主要做我需要做的事情。
        • @WRz 子引用可以直接调用父类中定义的方法,但这不会使方法成为虚拟方法。这只是语法糖化。您既不能覆盖静态方法,也可以通过在子类中提供具有相同签名的另一个实现来隐藏父实现。对于静态方法,要调用的方法实现是在编译时根据您用来访问它的引用的类型选择的,与虚拟方法相反,虚拟方法的实现是在运行时根据给定对象的实际类型选择的.
        • 好的,问题到此为止。感谢您快速而详尽的回答。讨论这个 Java '特性'的地方可能在其他地方......
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多