假设我有以下内容:
List<Apple> myList = new ArrayList<Apple>();
如果我想调用myList.add(myApple),Java 期望 frommyApple具有类型Apple,而不是任何其他类型(当然子类除外)。但是,如果我想要一个对象 ( Blender) 并根据内部声明的类型具有不同的方法签名<>并且没有方法重载怎么办?像这样:
Blender<Apple> Tool = new Blender<Apple>();
Tool.blendIt(new Apple()); //expects to have only apples in here
Blender<Banana> OtherTool = new Blender<Banana>();
OtherTool.blendIt(new Banana()); //only Banana's are permitted
可能吗?