问题描述
使用JavaFX作为最佳实践,可以使用通用BaseController继承所有后续UI控制器吗?否则会有一些问题?谢谢.
With JavaFX as a best practice, Is that fine to inherit all subsequent UI Controllers using a common BaseController or there will be some issues with it ? Thanks.
BaseController
public class BaseController implements Initializable {
protected URL location;
protected ResourceBundle rb;
@Override
public void initialize(URL location, ResourceBundle resources) {
this.location = location;
this.resources = resources;
}
}
HomeController
public class HomeController extends BaseController {
// Use BaseController related inherited properties and methods
}
推荐答案
我成功地使用了这种方法.该解决方案的优点在于,每个控制器可以封装自己的逻辑,同时还依赖于我想提供给所有控制器的通用功能.
I have used this approach with great success. The beauty of this solution was that each controller could encapsulate its own logic while also relying on common functionality I wanted to make available to all of my controllers.
我将基本控制器抽象化并实现了 Initializable
,并覆盖了子类控制器中的方法.现场注入也在子类级别上进行.
I made my base controller abstract and implement Initializable
and overrode the method in the subclass controller. Field injection was also handled on a subclass level.
这篇关于JavaFX作为使用通用基本控制器的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!