我正在构建一个JavaFX应用程序,我希望它实现Spring的SmartLifeCycle接口,以便在主类终止时执行任务。JavaFX主类必须扩展包含stop()方法的应用程序类。SmartLifeCycle接口还包含一个停止方法。看起来这两个方法拒绝共存,即使它们有不同的方法签名。从应用程序类扩展的JavaFX方法没有参数,并抛出异常,而来自SmartLifeCycle的实现方法则以Runnable对象作为参数。
这两种方法都可能存在于同一个类中吗?这两者都需要通过子类来实现,因此不管我做什么,编译器都会抱怨。
谢谢
发布于 2019-05-21 16:44:15
Application抽象类具有以下方法:
public void stop() throws Exception {}SmartLifecycle接口具有以下方法,继承自Lifecycle
void stop();正如您所看到的,一个可以抛出Exception,另一个不能抛出。如果要扩展Application并实现SmartLifecycle,则不能在重写的stop()方法中使用throws Exception。
public class MySpringJavaFxApp extends Application implements SmartLifecycle {
@Override
public void start(Stage primaryStage) throws Exception {
// ...
}
@Override
public void stop() {
// ...
}
// other methods that need to be implemented...
}但是请注意,您必须重写stop()以删除throws子句。否则,方法冲突(Application#stop不是抽象的,因此试图在这种情况下实现Lifecycle#stop )。
https://stackoverflow.com/questions/56242458
复制相似问题