N
Nomak
Hello!
i've just read some docs about generics in Java, and i would like to do
a complete Canvas class. Here is my attempt:
import java.util.LinkedList;
import java.util.List;
public class Canvas {
private List<Shape> shapeList = null;
class Shape {
public void draw() {
}
}
class SubShape extends Shape {
public void draw() {
}
}
public Canvas() {
shapeList = new LinkedList<Shape>();
add(new SubShape());
drawAll();
}
public void drawAll() {
// TEH UGLYNESS !!!
List<? extends Shape> shapeList = this.shapeList;
for (Shape s:shapeList)
s.draw();
}
public void add(Shape s) {
shapeList.add(s);
}
}
From what i've understood:
- unbounded is for writing
- bounded is for reading
So basically, i have to use temporaries or i have to do a
"drawAll(bounded version)" wich is even worse.
I really hope i've missed something because i think this stink.
Any advice/explaination apreciated
i've just read some docs about generics in Java, and i would like to do
a complete Canvas class. Here is my attempt:
import java.util.LinkedList;
import java.util.List;
public class Canvas {
private List<Shape> shapeList = null;
class Shape {
public void draw() {
}
}
class SubShape extends Shape {
public void draw() {
}
}
public Canvas() {
shapeList = new LinkedList<Shape>();
add(new SubShape());
drawAll();
}
public void drawAll() {
// TEH UGLYNESS !!!
List<? extends Shape> shapeList = this.shapeList;
for (Shape s:shapeList)
s.draw();
}
public void add(Shape s) {
shapeList.add(s);
}
}
From what i've understood:
- unbounded is for writing
- bounded is for reading
So basically, i have to use temporaries or i have to do a
"drawAll(bounded version)" wich is even worse.
I really hope i've missed something because i think this stink.
Any advice/explaination apreciated