のは、私はfolowingているとしましょう:使用ジェネリック
List<Apple> myList = new ArrayList<Apple>();
を私はmyList.add(myApple)
を呼び出したい場合は、JavaはApple
の種類、ありません(もちろんのサブクラスを除く)他のものを持っているmyApple
から期待しています。しかし、<>
との中で宣言された型に応じてオブジェクト(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
可能ですか?
'instanceof'演算子で' Banana'または 'Apple'のインスタンスがあるかどうかを調べることができます。 – enrybo
はい、できますが、 'Blender'の中に' Strawberry'を入れるとすぐに悪くなります:)フレキシブルな解決策が必要です – TomatoMato