我在我的域模型以下實體:Java的通用上界通配符錯誤
class Point {
public String details;
public Point() {
details = "empty";
}
public Point(String details) {
this.details = details;
}
}
class BasicPoint<X, Y> extends Point {
public X x;
public Y y;
public BasicPoint() {}
public BasicPoint(X x, Y y) {
this.x = x;
this.y = y;
}
}
class Collector<T> {
T elem;
public void collect(T elem) {
this.elem = elem;
}
}
我想對數據應用操作,並返回數據點或BasicPoint,如上聲明,但編譯器用下面的錯誤雖然OUT抱怨延伸點:
class Operator<OUT extends Point> {
public Collector<OUT> operate(Collector<OUT> collector) {
// compile error, collect(OUT) cannot be applied to BasicPoint<Integer, Integer>
collector.collect(new BasicPoint<Integer, Integer>(1,2));
return collector;
}
}
主要方法應該是這樣的:
Collector<BasicPoint<Integer, Integer>> c = new Collector<>();
c = new Operator<BasicPoint<Integer, Integer>>().operate(c);
System.out.println(c.elem.getClass() == new BasicPoint<Integer, Integer>().getClass());
你有代碼,打開IDE並生成輸出。 – user1933888
問題是代碼不能編譯,它在問題 – zequihg50
中定義的Operator類中顯示一個錯誤什麼是編譯器錯誤呢? –