Ho capito l'uso di extends nei generic ma non l'uso di super. Sul manuale c'è scritto:
E' possibile specificare anche un limite inferiore per un jolly aggiungendo una clausola super a una dichiarazione di jolly. Di seguito la forma generale:
<? super sottoclasse>
In questo caso sono argomenti accettabili solo le classi che sono superclassi di sottoclassi. Si tratta di una clausola esclusiva, perché non corrisponde alla classe specificata da sottoclasse.
Ho provato ad editare un codice messo a punto per extends e mi sorgono spontanee queste domande:
codice:
// Bounded Wildcard arguments.
// Two-dimensional coordinates.
class TwoD {
int x, y;
TwoD(int a, int b) {
x = a;
y = b;
}
}
// Three-dimensional coordinates.
class ThreeD extends TwoD {
int z;
ThreeD(int a, int b, int c) {
super(a, b);
z = c;
}
}
// Four-dimensional coordinates.
class FourD extends ThreeD {
int t;
FourD(int a, int b, int c, int d) {
super(a, b, c);
t = d;
}
}
// This class holds an array of coordinate objects.
class Coords<T extends TwoD> {
T[] coords;
Coords(T[] o) {
coords = o;
}
}
// Demonstrate a bounded wildcard.
class Esempio {
static void showXY(Coords<? super TwoD> c) {
System.out.println("X Y Coordinates:");
for (int i = 0; i < c.coords.length; i++)
System.out.println(c.coords[i].x + " " + c.coords[i].y);
System.out.println();
}
static void showXYZ(Coords<? extends ThreeD> c) {
System.out.println("X Y Z Coordinates:");
for (int i = 0; i < c.coords.length; i++)
System.out.println(c.coords[i].x + " " + c.coords[i].y + " "
+ c.coords[i].z);
System.out.println();
}
static void showAll(Coords<? extends FourD> c) {
System.out.println("X Y Z T Coordinates:");
for (int i = 0; i < c.coords.length; i++)
System.out.println(c.coords[i].x + " " + c.coords[i].y + " "
+ c.coords[i].z + " " + c.coords[i].t);
System.out.println();
}
public static void main(String args[]) {
TwoD td[] = { new TwoD(0, 0), new TwoD(7, 9), new TwoD(18, 4),
new TwoD(-1, -23) };
Coords<TwoD> tdlocs = new Coords<TwoD>(td);
FourD fd[] = { new FourD(1, 2, 3, 4), new FourD(6, 8, 14, 8),
new FourD(22, 9, 4, 9), new FourD(3, -2, -23, 17) };
Coords<FourD> fdlocs = new Coords<FourD>(fd);
ThreeD td3[] = { new ThreeD(3, 2, 3), new ThreeD(6, 0, 3),
new ThreeD(0, 3, 4), new ThreeD(8, 0, -8) };
Coords<ThreeD> tdlocs3 = new Coords<ThreeD>(td3);
System.out.println("Proviamo a capire super:");
showXY(tdlocs);
}
}
1.Perché sul manuale si dice che super è una clausola esclusica quando in realtà scrivendo <? super TwoD> e showXY(tdlocs); non ho problemi di compilazione. Il libro sta facendo un errore? (si/no)
codice:
// Bounded Wildcard arguments.
// Two-dimensional coordinates.
class TwoD {
int x, y;
TwoD(int a, int b) {
x = a;
y = b;
}
}
// Three-dimensional coordinates.
class ThreeD extends TwoD {
int z;
ThreeD(int a, int b, int c) {
super(a, b);
z = c;
}
}
// Four-dimensional coordinates.
class FourD extends ThreeD {
int t;
FourD(int a, int b, int c, int d) {
super(a, b, c);
t = d;
}
}
// This class holds an array of coordinate objects.
class Coords<T extends TwoD> {
T[] coords;
Coords(T[] o) {
coords = o;
}
}
// Demonstrate a bounded wildcard.
class Esempio {
static void showXY(Coords<? super TwoD> c) {
System.out.println("X Y Coordinates:");
for (int i = 0; i < c.coords.length; i++)
System.out.println(c.coords[i].x + " " + c.coords[i].y);
System.out.println();
}
static void showXYZ(Coords<? super FourD> c) {
System.out.println("X Y Z Coordinates:");
for (int i = 0; i < c.coords.length; i++)
System.out.println(c.coords[i].x + " " + c.coords[i].y + " "
+ c.coords[i].z);
System.out.println();
}
public static void main(String args[]) {
TwoD td[] = { new TwoD(0, 0), new TwoD(7, 9), new TwoD(18, 4),
new TwoD(-1, -23) };
Coords<TwoD> tdlocs = new Coords<TwoD>(td);
FourD fd[] = { new FourD(1, 2, 3, 4), new FourD(6, 8, 14, 8),
new FourD(22, 9, 4, 9), new FourD(3, -2, -23, 17) };
Coords<FourD> fdlocs = new Coords<FourD>(fd);
ThreeD td3[] = { new ThreeD(3, 2, 3), new ThreeD(6, 0, 3),
new ThreeD(0, 3, 4), new ThreeD(8, 0, -8) };
Coords<ThreeD> tdlocs3 = new Coords<ThreeD>(td3);
System.out.println("Proviamo a capire super:");
showXYZ(tdlocs3);
}
}
2.Con "showXYZ(Coords<? super FourD> c)" oppure "showXYZ(Coords<? super ThreeD> c)" e "showXYZ(tdlocs3);" java non funziona ThreeD e FourD sono classi che estendono altre classi e non sono superclassi? (si/no)
Se mettete due si allora ho capito diversamente mi dovete spiegare...
Non capisco perché non faccia un esempio chiaro anche con super...