「Java」extendsでジェネリクスクラスを使うサンプル
書式
T extends クラス
使用例
1.ジェネリクスクラスの定義
package com.study.arkgame;
public class Cft<T extends Number> {
private T value;
public Cft(T value) {
this.value = value;
}
/**
* @return value
*/
public T getValue() {
return value;
}
/**
* @param value セットする value
*/
public void setValue(T value) {
this.value = value;
}
}
2,mainクラスの実行
package com.study.arkgame;
public class TestInfo {
private static final int cnt = 123;
private static final int tb = 456;
public static void main(String[] args) {
// コンストラクタ
Cft<Integer> cftA = new Cft<Integer>(cnt);
Integer res = cftA.getValue();
System.out.println("コンストラクタで値を指定: " + res);
// メソッドを利用
cftA.setValue(tb);
Integer resB = cftA.getValue();
System.out.println("メソッドで値を指定: " + resB);
}
}
実行結果
コンストラクタで値を指定: 123
メソッドで値を指定: 456