简体   繁体   English

在Java中扩展主类

[英]Extending the main class in Java

I need to extend the functionality of my main class, by overriding some of its methods. 我需要通过覆盖它的一些方法来扩展我的主类的功能。 I was expecting that the class extending the main class would be able to be run. 我期待扩展主类的类能够运行。 However, Eclipse doesn't recognize MyLauncher as a runnable class. 但是,Eclipse不会将MyLauncher识别为可运行的类。 In the following code, I have a setup() method that is overridden by the subclass. 在下面的代码中,我有一个由子类重写的setup()方法。 What I want is a way to run the main(..) from the super class but also the setup from the subclass. 我想要的是一种从超类运行main(..) ,还有从子类运行的设置。

// Launcher.java
public class Launcher {

    Launcher instance;

    public static void main (args[]) {
        instance = new Launcher(); // This is likely the problem
        instance.setup();
    }

    public void setup() {
        System.out.println("Default agent setup.");
    }
}

// MyLauncher.java
public class MyLauncher extends Launcher {

    public void setup() {
        System.out.println("New agent setup!");
    }
}

I accept alternatives to this. 我接受这个的替代方案。 I can't add a main method to the subclass, though. 但是我无法在子类中添加main方法。 The Launcher class is inside an API i'm making, so it can't refer to the class MyLauncher that is using the API. Launcher类在我正在制作的API中,因此它不能引用正在使用API​​的类MyLauncher。

edit: I think this is to specific to my problem. 编辑:我认为这是针对我的问题。 I decided to search for a new approach. 我决定寻找新的方法。 Since I'm working with JDT, I'm going to parse the Launcher and inject the class. 由于我正在使用JDT,我将解析Launcher并注入该类。

Static methods are not inherited, they're always bound to the class that defines them, and need to be called explicitely. 静态方法不是继承的,它们总是绑定到定义它们的类,并且需要明确地调用。

In you case, the MyLauncher needs a main() method too, and could then delegate to the main() method of Launcher : 在你的情况下, MyLauncher需要一个main()方法,然后可以委托Launchermain()方法:

public class MyLauncher extends Launcher {

    public static void main (String[] args) {
        Launcher.main(args);
    }

    protected void setup() {
        System.out.println("New agent setup!");
    }
}

Protected methods can not be called from outside. 无法从外部调用受保护的方法。 So the MyLauncher.setup() do not override Launcher.setup() and instance.setup(); 所以MyLauncher.setup()不会覆盖Launcher.setup()instance.setup(); calls the public method from Class Launcher . 从Class Launcher调用public方法。

There can only be one main method in your project, that is one entry point to the program. 项目中只能有一个主要方法,即程序的一个入口点。 So let's assume you're going to be keeping the main method in the Launcher class. 因此,我们假设您将在Launcher类中保留main方法。

Your main method signature should be: 您的主要方法签名应该是:

public static void main (String args[])

And unless you want the setup() method from the launcher to be called you'd want to do: 除非你想要调用启动器的setup()方法,否则你想做:

instance = new MyLauncher();

That would call the setup() method from MyLauncher. 这将从MyLauncher调用setup()方法。

If you want to call setup() from the Launcher class you need to instantiate the launcher class: 如果要从Launcher类调用setup(),则需要实例化启动器类:

instance = new Launcher();

If you want to be able to run MyLauncher.setup() , the variable must be a MyLauncher . 如果您希望能够运行MyLauncher.setup() ,则该变量必须是MyLauncher You are initializing and storing a Launcher in the main() function. 您正在main()函数中初始化并存储Launcher

If the two classes are in the same package, or Launcher.java imports the MyLauncher class, then the main() function in Launcher should be able to be: 如果这两个类在同一个包中,或者Launcher.java导入了MyLauncher类,那么Launchermain()函数应该是:

public class Launcher {
    Launcher instance;

    public static void main(String[] args) {
        instance = new MyLauncher();
        if(instance instanceof MyLauncher) {
            ((MyLauncher) instance).setup();
        } else
        {
            instance.setup();
        }
    }
}

As you say, the fact that you create an instance of Launcher directly in main means that no inheritance is available. 正如您所说,直接在main创建Launcher实例的事实意味着没有可用的继承。 Even if you could start MyLauncher easily from Eclipse, within the main method you wouldn't know which type had actually been used to start it. 即使您可以轻松地从Eclipse启动MyLauncher ,在main方法中您也不会知道实际使用了哪种类型来启动它。 I can't see any easy solution that doesn't involve either creating a main method in each class or providing the class name as a command-line argument. 我看不到任何简单的解决方案不涉及任一创建main中的每个类的方法提供类名作为一个命令行参数。 I would probably separate the "running" from anything else: 我可能会将“跑步”与其他任何东西分开:

public class Launcher {
    public static void launch(LaunchConfiguration configuration) {
        configuration.setup();
        ...
    }
}

public class LaunchConfiguration {
    public static void main(String[] args) {
        Launcher.launch(new LaunchConfiguration());
    }

    public void setup() {
    }
}

public class MyLaunchConfiguration {
    public static void main(String[] args) {
        Launcher.launch(new MyLaunchConfiguration());
    }

    @Override
    public void setup() {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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