「Java」上限付き境界ワイルドカードextendsのサンプル
書式
クラス名<? extends Number>変数名
使用例
package com.arkgame.demo;
//ジェネリック型クラスの定義
class Cft<T> {
private T fm;
// コンストラクタ
public Cft(T arg) {
this.fm = arg;
}
// T型fmの値を返す
public T getFunc() {
return fm;
}
}
public class GenericDemo {
public static void main(String[] args) {
// Integer型のジェネリックス
Cft<Integer> intObj = new Cft<Integer>(3456);
Integer itA = intObj.getFunc();
System.out.println("Integer型のジェネリックス: " + itA);
// ワイルドカード extends
Cft<? extends Number> itB = intObj;
Number td = itB.getFunc();
System.out.println("Number型のジェネリックス: " + td);
}
}
実行結果
Integer型のジェネリックス: 3456
Number型のジェネリックス: 3456