Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类<;T扩展另一类>;方法仿制药_Java_Generics_Inheritance_Casting_Abstraction - Fatal编程技术网

Java 类<;T扩展另一类>;方法仿制药

Java 类<;T扩展另一类>;方法仿制药,java,generics,inheritance,casting,abstraction,Java,Generics,Inheritance,Casting,Abstraction,我有这门课 public class ClusterMapPresenter<T extends ClusterItem>{ private ClusterMapView<T> clusterMapView; public ClusterMapPresenter(ClusterMapView<T> clusterMapView){ this.clusterMapView = clusterMapView; }

我有这门课

public class ClusterMapPresenter<T extends ClusterItem>{

    private ClusterMapView<T> clusterMapView;

    public ClusterMapPresenter(ClusterMapView<T> clusterMapView){
        this.clusterMapView = clusterMapView;
    }

    public void createView() {
        setItems(getMockItems());
    }

    private List<T> getMockItems() {
        List<T> items = new ArrayList<>();
        items.add( new SpeditionClusterItem(new Spedition(Constants.MOCK)));
        return items;
    }   

    public void setItems(List<T> clusterItems){
        clusterMapView.setMarkers(clusterItems);
    }
}
然而,我并不喜欢这种方法,有没有更好的方法来设计这个类

我将添加以下代码:

public interface ClusterMapView<T extends ClusterItem> extends BaseView {
    public void setMarkers(List<T> markers);

}
公共接口ClusterMapView扩展了BaseView{
公共无效设置标记(列表标记);
}
此接口在以下活动中实现:

public class Activity implements ClusterMapView<SpeditionClusterItem> {

    private ClusterMapPresenter<SpeditionClusterItem> mClusterMapPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
...
        mClusterMapPresenter = new ClusterMapPresenter<>(this);
...        
    }


    @Override
    public void setMarkers(List<SpeditionClusterItem> markers) {
        mMapFragment.addItemsToMap(markers);

    }

}
公共类活动实现ClusterMapView{
私人ClusterMapPresenter McClusterAppresenter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
...
mclusterAppresenter=新的ClusterMapPresenter(此);
...        
}
@凌驾
公共无效设置标记(列表标记){
mMapFragment.additemsomep(标记);
}
}

关键是,我想让活动通过在实现中设置的参数来显示方法。

好吧,我通过将列表检索与演示者分离,在构造函数中添加列表来解决它

public ClusterMapPresenter(ClusterMapView<T> clusterMapView, List<T> clusterItems){
        this.clusterMapView = clusterMapView;
        this.clusterItems = clusterItems;
    }
public ClusterMapPresenter(ClusterMapView ClusterMapView,列表clusterItems){
this.clusterMapView=clusterMapView;
this.clusterItems=clusterItems;
}

我不是很确定这是一个好的设计,但它的作品。欢迎对如何改进它提出任何建议。

如果您创建了
新的ClusterMapPresenter()。getMockItems()
?返回的列表将显示为
list
,但将包含
SpeeditionClusterItem
值。这不是泛型的使用方式。在
ClusterMapPresenter
中,您不知道
T
的类型是什么,但您的代码对此进行了假设。如果你能更清楚地解释你为什么要这么做,我们也许能提出一个替代方案。可能没有理由一开始就使用泛型。问题是,我目前正在使用new
ClusterMapPresenter(this)
的活动中使用泛型,我希望将结果方法显示为列表(这就是我希望使用泛型的原因)我将发布更多的代码片段。如果您知道生成的
List
应该始终是
List
,为什么您认为需要泛型?发布更多的代码可能会有所帮助,但同样,请更清楚地解释您正在尝试做什么以及为什么。
public ClusterMapPresenter(ClusterMapView<T> clusterMapView, List<T> clusterItems){
        this.clusterMapView = clusterMapView;
        this.clusterItems = clusterItems;
    }