如何使方法返回类型为泛型?
Animal
Animal
Dog
, Duck
, Mouse
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); }}
Mouse 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
富国沪深
ABOUTYOU
素胚勾勒不出你
相关分类