[英]Inheriting ArrayList property from superclass but assigning different types to it in child classes
[英]Override property types in child classes in java android
我正在尝试使用Android中的MVP(模型-视图-演示器)模式来实现我的应用程序,但是我遇到了问题。 我想要一个抽象的基本演示者,它的视图类属性为通用类型ViewInterface
但是在创建真正的演示者时,我希望此属性为RealPresenterView
类型,并且在可能的情况下,无需覆盖所有相同的函数主持人。
因为我可能没有正确解释自己,所以这里有一些代码来说明这种情况,首先是基本演示者:
abstract public class BasePresenter {
protected ViewInterface view;
protected ViewInterface getView() {
return view;
}
}
然后是真正的主持人
public class RealPresenter {
protected RealPresenterView view;
// ...
view.methodOnlyExistingInRealPresenterView();
}
我希望能够在RealPresenter中将视图用作RealPresenterView,而无需在类中添加很多代码。 那可能吗?
我已经阅读了有关类模板的信息,但是在没有在每个演示者中添加功能的情况下,我还没有看到如何实现。
您可以使用泛型:
abstract public class BasePresenter<T extends ViewInterface> {
protected T view;
protected T getView() {
return view;
}
}
public class RealPresenter extends BasePresenter<RealPresenterView> {
}
请注意,我有一段时间没有使用Java编写代码了,但是我认为还可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.