Generics et Types Incompatibles
Je veut permettre aux utilisateurs de ma classe d'iterer sur les points qu'elle contient, mais pas de changer ces points. J'ai donc cree une interface StaticPoint:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
interface StaticPoint {
int getx();
int gety();
}
class Point implements StaticPoint{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getx(){ return x; }
public int gety(){ return y; }
} |
Dans ma classe j'ai:
Code:
1 2 3 4 5 6
|
ArrayList<Point> points;
public StaticPoint point(int i){
return points.get(i);
} |
Tout cela fonctionne bien, mais pas mon iterateur ne compile pas:
Code:
1 2 3 4
|
public Iterator<StaticPoint> iterator(){
return points.iterator();
} |
Code:
1 2 3 4 5 6
|
incompatible types
found : java.util.Iterator<Point>
required: java.util.Iterator<StaticPoint>
return points.iterator();
^ |
Est-il possible de contourner cette limitation?