Re: Extracting Class names in Abstract classes with Generics.
Ian Wilson wrote:
Hemal Pandya wrote:
Ian Wilson wrote:
I have this working:
[....]
class Zap extends Foo<Apple> {
fooFun(new Apple());
// I want my Audit log to say "Apple had ..."
Far as I can tell it already says that. Can you post actual code sample
that compiles and runs? Please use System.out.println in place of
Audit.
<SSCCE snipped>
The following does what I want but it is unlikely to be of interest to
anyone else as it doesn't involve generics or cleverness. I include it
for completeness :-)
------------------------------------8<----------------------------------
public class ProblemSolved {
public static void main(String[] args) {
Zap zap = new Zap();
Zog zog = new Zog();
}
}
abstract class Foo<E> {
Foo() { }
void fooFun(E o) {
AuditLog.memo("fooFunned a ", o);
}
void fooFiz(E o) {
AuditLog.memo("fooFizzed a ", o);
}
void fooFar(E o) {
AuditLog.memo("fooFarred a ", o);
}
void fooFie(E o) {
AuditLog.memo("fooFied a ", o);
}
}
class Apple {
String colour;
Apple(String colour) {
this.colour = colour;
}
public String toString() {
return "A " + colour + " apple";
}
}
class Brick {
int weight;
Brick(int weight) {
this.weight = weight;
}
public String toString() {
return "A brick weighing " + weight;
}
}
class Zap extends Foo<Apple> {
Zap() {
Apple apple = new Apple("Red");
fooFun(apple);
fooFar(apple);
}
}
class Zog extends Foo<Brick> {
Zog() {
Brick brick = new Brick(12);
fooFun(brick);
fooFiz(brick);
fooFie(brick);
}
}
class AuditLog {
static void memo(String action, Object o) {
System.out.println(action + o.getClass().getSimpleName()
+ " (" + o.toString() + ")");
}
}
------------------------------------8<----------------------------------