- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我有这个错误:
incompatibles types: List<Car> cannot be converted to Iterable<Iterator>
incompatibles types: List<Truck> cannot be converted to Iterable<Iterator>
Car 类扩展了 Vehicle 类。卡车还扩展了车辆。我必须创建可迭代的 Vehicle 类??
public static void print(Iterable<Vehicle> it){
for(Vehicle v: it) System.out.println(v);
}
public static void main(String[] args) {
List<Car> lcotxe = new LinkedList<Car>();
List<Truck> lcamio = new LinkedList<Truck>();
print(lcotxe);//ERROR
print(lcamio);//ERROR
}
最佳答案
这无法编译,因为 List<Car>
不是 Iterable<Vehicle>
的子类型.
然而,它是 Iterable<? extends Vehicle>
的子类型。这称为covariance .
public static void print(Iterable<? extends Vehicle> it){
for(Vehicle v: it) System.out.println(v);
}
您还可以选择使该方法通用。
public static <A extends Vehicle> void print(Iterable<A> it){
for(Vehicle v: it) System.out.println(v);
}
关于java - 为什么我不能将 List<Truck> 用作 Iterable<Vehicle>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37377616/
我们正在构建用于卡车导航的移动应用程序。在为卡车使用 HERE SDK 路线计算时,它有时会尝试避开特定路段,这实际上适合卡车。 一个例子 - 高速公路交叉口: 当我计算汽车的路线时,路线是 corr
这个问题已经有答案了: Compile error when providing interface as an arraylist type (3 个回答) 已关闭 7 年前。 嗨,我有这个错误:
我想获得一组卡车和取货点之间的距离。我看到有用于卡车路线的 REST API,还有用于距离矩阵的 API。对两个 API 的请求的 URL 不同。从距离矩阵 API 的微软文档中,我可以看到 trav
我想在 Xcode 9 中以编程方式重新映射以下相机控件: 卡车 - alt + LMB 偏航 - LMB 推车 - alt + MMB Truck 沿 X 轴拖动虚拟相机,Yaw 绕 Y 轴旋转相机
实际网址如下 https://xxa-sa-usedtrucks.abc.com/api/vehicles?itemsPerPage=20&facets=r=US;g=truck 当我开火时我会得到结
我是一名优秀的程序员,十分优秀!