作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在该类的方法中实例化一个泛型类,但遇到编译时错误。希望有人可以在这里提供一些见解:
//returns a new ILo<T> with all items in this list that satisfy
//the given predicate
public ILo<T> filter(ISelect<T> pred);
// Represents a nonempty list of items of type T
class ConsLo<T> implements ILo<T>{
T first;
ILo<T> rest;
//returns a new ILo<T> with all items in this list that satisfy
//the given predicat
public ILo<T> filter(ISelect pred) {
return new ConsLo<T>(pred.select(this.first),
this.rest.filter(pred));
}
我提供了方法的接口(interface)定义,然后是 ConsLo 类的定义,最后是我正在处理的方法声明。我不明白如何在保持通用性的同时实例化此类,以便与任何类型和谓词 pred 一起使用。这是编译器错误:
ILo.java:95: error: method select in interface ISelect<T#3> cannot be applied to given types;
return new ConsLo<T>(pred.select(this.first),
^
required: T#1
found: T#2
reason: actual argument T#2 cannot be converted to T#1 by method invocation conversion
where T#1,T#2,T#3 are type-variables:
T#1 extends Object declared in method <T#1>filter(ISelect<T#1>)
T#2 extends Object declared in class ConsLo
T#3 extends Object declared in interface ISelect
最佳答案
您应该使用 ISelect
的通用版本:
public ILo<T> filter(ISelect<T> pred) {
return new ConsLo<T>(pred.select(this.first),
this.rest.filter(pred));
}
这边走pred
将是ISelect<T>
,而不是 ISelect - 这是两种类型 T#1
和T#2
编译器正在提示。
关于java - 在该类的方法体内实例化泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274165/
因此,我的flutter应用程序运行正常,但是我想对其进行一些更改。 我要执行的第一个更改是创建一个水平滚动窗口小部件,该窗口小部件的图像可以单击以更改工作站。 但是要做到这一点,我首先需要将两个小部
如何在 Bootstrap 模态的主体内设置 div 样式?这是我的模型: text ') .okBtn('ok') .open();">Open Model 我的CSS: @M
我有一个简单的触发器,它在 SQL Fiddle 上运行,但它不允许我将 INSERT STATEMENT 移动到触发器的主体内。 my code on sqlFiddle我只是想移动这条线 INSE
我是一名优秀的程序员,十分优秀!