我如何使方法返回类型通用? [英] How do I make the method return type generic?
问题描述
考虑这个例子(典型的OOP书籍):
我有一个 Animal
类,其中每个 Animal
可以有很多朋友。
和 Dog ,
Duck ,
鼠标
等添加了特定行为,如 bark()
, quack()
等。
以下是 Animal
类:
public class Animal {
private Map< String,Animal> friends = new HashMap<>();
public void addFriend(String name,Animal animal){
friends.put(name,animal);
}
public Animal callFriend(String name){
return friends.get(name);
$ b $ p
$ b 以下是一些包含大量类型转换的代码片段:
鼠标jerry = new Mouse();
jerry.addFriend(spike,new Dog());
jerry.addFriend(quacker,new Duck());
((Dog)jerry.callFriend(spike))。bark();
((Duck)jerry.callFriend(quacker))。quack();
有没有什么办法可以使用泛型作为返回类型来摆脱类型转换,所以我可以说
jerry.callFriend(spike)。bark();
jerry.callFriend(quacker)。quack();
以下是一些初始代码,将返回类型作为从未使用过的参数传递给方法。
public< T extends Animal> T callFriend(String name,T unusedTypeObj){
return(T)friends.get(name);
有没有一种方法可以在没有额外参数的情况下计算出运行时的返回类型使用 instanceof
?或者至少通过传递一个类型的类而不是一个虚拟实例。
我明白泛型是用于编译时类型检查的,但是是否有解决方法?
pre> public< T extends Animal> T callFriend(String name,Class< T> type){
return type.cast(friends.get(name));
}
然后调用它:
jerry.callFriend(spike,Dog.class).bark();
jerry.callFriend(quacker,Duck.class).quack();
此代码具有不产生任何编译器警告的好处。当然,这实际上只是从通用前的日子开始的更新版本,并没有增加任何额外的安全性。
Consider this example (typical in OOP books):
I have an Animal
class, where each Animal
can have many friends.
And subclasses like Dog
, Duck
, Mouse
etc which add specific behavior like bark()
, quack()
etc.
Here's the Animal
class:
public class Animal {
private Map<String,Animal> friends = new HashMap<>();
public void addFriend(String name, Animal animal){
friends.put(name,animal);
}
public Animal callFriend(String name){
return friends.get(name);
}
}
And here's some code snippet with lots of typecasting:
Mouse jerry = new Mouse();
jerry.addFriend("spike", new Dog());
jerry.addFriend("quacker", new Duck());
((Dog) jerry.callFriend("spike")).bark();
((Duck) jerry.callFriend("quacker")).quack();
Is there any way I can use generics for the return type to get rid of the typecasting, so that I can say
jerry.callFriend("spike").bark();
jerry.callFriend("quacker").quack();
Here's some initial code with return type conveyed to the method as a parameter that's never used.
public<T extends Animal> T callFriend(String name, T unusedTypeObj){
return (T)friends.get(name);
}
Is there a way to figure out the return type at runtime without the extra parameter using instanceof
? Or at least by passing a class of the type instead of a dummy instance.
I understand generics are for compile time type-checking, but is there a workaround for this?
解决方案 You could define callFriend
this way:
public <T extends Animal> T callFriend(String name, Class<T> type) {
return type.cast(friends.get(name));
}
Then call it as such:
jerry.callFriend("spike", Dog.class).bark();
jerry.callFriend("quacker", Duck.class).quack();
This code has the benefit of not generating any compiler warnings. Of course this is really just an updated version of casting from the pre-generic days and doesn't add any additional safety.
这篇关于我如何使方法返回类型通用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!