gpt4 book ai didi

java - List 上的通用类型兼容性错误,其中 T 是接口(interface)

转载 作者:行者123 更新时间:2023-12-02 12:50:06 25 4
gpt4 key购买 nike

我有这个java代码:

tgia = new ThumbsGalleryAdapter(this, profilePositionsOverlay.items);

我收到此错误:

The constructor ThumbsGalleryAdapter(SharePositionMapActivity, List) is undefined SharePositionMapActivity.java /com.shopzeus.android.majorforms.activity.MainActivity/src/com/shopzeus/android/majorforms/activity line 114 Java Problem

ThumbsGalleryAdapter的构造函数是:

public ThumbsGalleryAdapter(Activity aActivity, List<ThumbsGalleryItem> aItems)

profilePositionsOverlay.items 的类型:

List<SharedPositionMapItem>

名称ThumbsGalleryItem指的是一个接口(interface):

public interface ThumbsGalleryItem {
public String getTitle();
public String getRemotePath();
}

最后,SharedPositionMapItem 是一个实现 ThumbsGalleryItem 的类:

private static class SharedPositionMapItem extends OverlayItem implements ThumbsGalleryItem

问题当然是:为什么 List<SharedPositionMapItem>List<ThumbsGalleryItem> 类型不兼容?因为SharedPositionMapItem implements ThumbsGalleryItem ,我希望它们能够兼容。解决办法是什么?

最佳答案

public ThumbsGalleryAdapter(Activity aActivity, List<? extends ThumbsGalleryItem> aItems)

更多详情请参阅: http://codeidol.com/java/javagenerics/Subtyping-and-Wildcards/The-Get-and-Put-Principle/和这个 Explanation of the get-put principle

关于java - List<T> 上的通用类型兼容性错误,其中 T 是接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13050494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com