「Java」下限付け境界ワイルドカードsuperのサンプル

2021年9月2日

書式
クラス名<? super Integer>変数名

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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) {
// Object型のジェネリックス
Cft<Object> objA = new Cft<Object>(200);
// ワイルドカードsuper
Cft<? super Integer> intObj;
intObj = objA;
// 値を取得
Object res = intObj.getFunc();
System.out.println("Integer型のジェネリックス: " + res);
}
}
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) { // Object型のジェネリックス Cft<Object> objA = new Cft<Object>(200); // ワイルドカードsuper Cft<? super Integer> intObj; intObj = objA; // 値を取得 Object res = intObj.getFunc(); System.out.println("Integer型のジェネリックス: " + res); } }
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) {

            // Object型のジェネリックス
            Cft<Object> objA = new Cft<Object>(200);

            // ワイルドカードsuper
            Cft<? super Integer> intObj;
            intObj = objA;

            // 値を取得
            Object res = intObj.getFunc();
            System.out.println("Integer型のジェネリックス: " + res);

      }

}

結果
Integer型のジェネリックス: 200

Java

Posted by arkgame