-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericExample.java
More file actions
34 lines (27 loc) ยท 927 Bytes
/
GenericExample.java
File metadata and controls
34 lines (27 loc) ยท 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package ch13.sec01;
public class GenericExample {
public static void main(String[] args) {
// ๋ฐ์ค ์์ฑ์ ํ์
ํ๋ผ๋ฏธํฐ T ๋์ ๋ค๋ฅธ ํ์
(String, Integer)์ผ๋ก ๋์ฒด
// 1๋ฒ
/*Box<String> box1 = new Box<String>();
Box<Integer> box2 = new Box<Integer>();*/
// 2๋ฒ
/*Box<String> box1 = new Box<>();
Box<Integer> box2 = new Box<>();*/
// 3๋ฒ
var box1 = new Box<String>();
var box2 = new Box<Integer>();
box1.content = "hi";
box2.content = 100;
String str = box1.content;
Integer i = box2.content;
System.out.println("str = " + str);
System.out.println("i = " + i);
System.out.println("box1.content = " + box1.content);
System.out.println("box2.content = " + box2.content);
}
}
// ํ์
ํ๋ผ๋ฏธํฐ๋ก T ์ฌ์ฉ
class Box<T> {
public T content;
}