ジェネリクスです。宣言時に指定します。
Java
1class UselessGeneric<T1, T2> {
2 private T1 attr1;
3 private T2 attr2;
4
5 UselessGeneric(T1 arg1, T2 arg2) {
6 this.attr1 = arg1;
7 this.attr2 = arg2;
8 }
9
10 void print() {
11 System.out.println(attr1);
12 System.out.println(attr2);
13 System.out.println("--------------------");
14 }
15}
16
17class Main {
18 public static void main(String[] args) {
19 UselessGeneric<Integer, Double> ug1 = new UselessGeneric<>(1, 2.0);
20 ug1.print();
21
22 UselessGeneric<Boolean, String> ug2 = new UselessGeneric<>(true, "hoge");
23 ug2.print();
24 }
25}
実行結果 Wandbox
1
2.0
--------------------
true
hoge
--------------------
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/03 12:00
2019/10/03 12:05