如何使用两个泛型类型来实现一个接口的Java类? [英] How to make a Java class that implements one interface with two generic types?
问题描述
我有一个通用接口
public interface Consumer< E> {
public void consume(E e);
}
我有一个类使用两种类型的对象,所以我想做类似的事情:
public class TwoTypesConsumer implements Consumer< Tomato> ;, Consumer< Apple>
{
public void consume(Tomato t){.....}
public void consume(Apple a){......}
}
显然我做不到。
我当然可以自己实现调度,例如
public class TwoTypesConsumer implements Consumer< Object> {
public void consume(Object o){
if(o instanceof Tomato){.....}
else if(o instanceof Apple){.....}
else {throw new IllegalArgumentException(...)}
}
}
<但是我正在寻找泛型提供的编译时类型检查和调度解决方案。我可以想到的最好的解决方案是定义单独的接口,例如
public interface AppleConsumer {
public void consume(Apple a);
}
在功能上,我认为这个解决方案是可以的。它只是冗长而丑陋。
任何想法?
考虑封装:
public class TwoTypesConsumer {
private TomatoConsumer tomatoConsumer = new TomatoConsumer();
私人AppleConsumer appleConsumer =新AppleConsumer();
public void consume(Tomato t){
tomatoConsumer.consume(t);
}
public void consume(Apple a){
appleConsumer.consume(a);
}
public static class TomatoConsumer实现Consumer< Tomato> {
public void consume(Tomato t){.....}
}
public static class AppleConsumer implements Consumer< Apple> {
public void consume(Apple a){.....}
}
}
如果创建这些静态内部类困扰您,您可以使用匿名类:
public class TwoTypesConsumer {
私人消费者<番茄> tomatoConsumer =新的消费者<番茄>(){
公共无效消费(番茄){
}
};
私人消费者< Apple> appleConsumer = new Consumer< Apple>(){
public void consume(Apple a){
}
};
public void consume(Tomato t){
tomatoConsumer.consume(t);
}
public void consume(Apple a){
appleConsumer.consume(a);
}
}
I have a generic interface
public interface Consumer<E> {
public void consume(E e);
}
I have a class that consumes two types of objects, so I would like to do something like:
public class TwoTypesConsumer implements Consumer<Tomato>, Consumer<Apple>
{
public void consume(Tomato t) { ..... }
public void consume(Apple a) { ...... }
}
Apparently I can't do that.
I can of course implement the dispatch myself, e.g.
public class TwoTypesConsumer implements Consumer<Object> {
public void consume(Object o) {
if (o instanceof Tomato) { ..... }
else if (o instanceof Apple) { ..... }
else { throw new IllegalArgumentException(...) }
}
}
But I am looking for the compile-time type-checking and dispatching solution that generics provide.
The best solution I can think of is to define separate interfaces, e.g.
public interface AppleConsumer {
public void consume(Apple a);
}
Functionally, this solution is OK, I think. It's just verbose and ugly.
Any ideas?
Consider encapsulation:
public class TwoTypesConsumer {
private TomatoConsumer tomatoConsumer = new TomatoConsumer();
private AppleConsumer appleConsumer = new AppleConsumer();
public void consume(Tomato t) {
tomatoConsumer.consume(t);
}
public void consume(Apple a) {
appleConsumer.consume(a);
}
public static class TomatoConsumer implements Consumer<Tomato> {
public void consume(Tomato t) { ..... }
}
public static class AppleConsumer implements Consumer<Apple> {
public void consume(Apple a) { ..... }
}
}
If creating these static inner classes bothers you, you can use anonymous classes:
public class TwoTypesConsumer {
private Consumer<Tomato> tomatoConsumer = new Consumer<Tomato>() {
public void consume(Tomato t) {
}
};
private Consumer<Apple> appleConsumer = new Consumer<Apple>() {
public void consume(Apple a) {
}
};
public void consume(Tomato t) {
tomatoConsumer.consume(t);
}
public void consume(Apple a) {
appleConsumer.consume(a);
}
}
这篇关于如何使用两个泛型类型来实现一个接口的Java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!