LoginSignup
0
0

More than 5 years have passed since last update.

てはじめに(編集中)

Last updated at Posted at 2017-06-20

genericsの基本について

例)

List<String> list = new ArrayList<>();
list.add("aaa");
list.add(1); //コンパイルエラー
  • List に 入る型を明示できる

ジェネリック型クラス内で型変数Eをnewするのは禁止

class Owner<E>{
  E createObject(){
    return new E();
  }
}

コンパイルエラー

  • タスク1
  • タスク2

Qiita

型パラメータ
<>の中の変数部分。
型変数
型パラメータに修飾される変数
型引数
ジェネリック型を使用する際に<>に渡す具体的な型。ListのString部分
パラメータ化された型
型引数を渡して実際にしようできる型。Listそのもの

List を宣言すると

class List<String> {
    private String element;
    String get() {
        return element;
    }
    void put(String e) {
        this.element = e;
    }
}

が内部的に創出される。(未確定)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0