interface on the fly
On rahel's suggestion I thought I should post something really useful that you can do with generics. In java you can map most things by just adding another interface but sometimes you really don't want to define a crappy interface like:
interface DisposableComponent {
void dispose();
Component getComponent();
}
So here's something that not everyone knows about generics:
interface Flappable {
void flap();
}
interface Flippable {
void flip();
}
class FlipFlapper {
<T extends Flappable & Flippable> void flipAndFlap(T ff) {
ff.flip();
ff.flap();
}
}
interface DisposableComponent {
void dispose();
Component getComponent();
}
So here's something that not everyone knows about generics:
interface Flappable {
void flap();
}
interface Flippable {
void flip();
}
class FlipFlapper {
<T extends Flappable & Flippable> void flipAndFlap(T ff) {
ff.flip();
ff.flap();
}
}
1 Comments:
thanks nj! for all remaining questions, angelika langer's java generics faq is usually quite helpful...
By
rahel luethy, at 1:46 pm
Post a Comment
<< Home