Java generics: actual class as a generic parameter.
Posted
by
user554916
on Stack Overflow
See other posts from Stack Overflow
or by user554916
Published on 2010-12-27T11:49:27Z
Indexed on
2010/12/27
11:54 UTC
Read the original article
Hit count: 153
What do I write instead of "TheClass" to make this work? Or is there an alternative way to do it (possibly without making WithName and WithAge generic)?
class Item {
NeigborList<TheClass> neighbors;
}
class WithName extends Item { // here I want neighbors to be a NeighborList<WithName>
String name;
void someMethod() {
System.out.println(neighbors.nearestTo(this).name);
}
}
class WithAge extends Item { // here I want neighbors to be a NeighborList<WithAge>
int age;
void someOtherMethod() {
System.out.println(neighbors.nearestTo(this).age);
}
}
© Stack Overflow or respective owner