関数のパラメーターから T を取得して、Vector の型を定義するにはどうすればよいですか? 例:
public void addPlot(String plotName, int minX, int maxX,
int minY, int maxY, PlotStyle plotStyle, Class<? extends Number> type)
{
Vector<type.class> dataset = new Vector<type.class>();
}
まず、Java では、「テンプレート」(C++ 用語) ではなく「ジェネリック」です。
次に、名前付きのジェネリック型パラメーターを使用してメソッドをジェネリックにし、それを使用します。
// generic declaration after public, before void
public <T extends Number> void addPlot(String plotName, int minX, int maxX,
int minY, int maxY, PlotStyle plotStyle, Class<T> type)
{
Vector<T> dataset = new Vector<T>();
}
これ<T extends Number>はジェネリック型パラメーターの宣言 (上限あり) であり、<T>表示される他の場所はそれを使用する場所です。