Skip to content

Commit 1d05250

Browse files
authored
Update Chapter54.java
1 parent 7e92283 commit 1d05250

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Chapter54.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package chapter54;
2+
//Generic<String> gen = new Generic<String>();
3+
class Generic<T>{
4+
T[] v;
5+
void set(T[] n) {
6+
v= n;
7+
}
8+
T[] get() {
9+
return v;
10+
}
11+
void print() {
12+
for(T o : v) {
13+
System.out.print(o+" ");
14+
}
15+
System.out.println();
16+
}
17+
}
18+
19+
20+
21+
22+
23+
public class GenericEx {
24+
25+
public static void main(String[] args) {
26+
Generic<String> gen = new Generic<String>();
27+
String[] ss = {"홍길동","이순신","김유신"};
28+
//String 제네릭 타입의 매게변수에 String 배열객체 전달
29+
gen.set(ss);
30+
gen.print();
31+
System.out.println("---------------------");
32+
for(String s : gen.get()) {
33+
System.out.println(s);
34+
}
35+
System.out.println("----------------------");
36+
Generic<Integer> gen2 = new Generic<Integer>();
37+
Integer[] ii = {1,2,3};
38+
//String 제네릭 타입의 매게변수에 Integer 배열객체 전달
39+
//nogen.set(ii);//에러발생
40+
gen2.set(ii);
41+
//gen2.set(ss);//String형이므로 에러 발생
42+
gen2.print();
43+
System.out.println("---------------------");
44+
for(Integer o : gen2.get()) {
45+
//String s = (String) o //형변환 필요없음
46+
System.out.println(o);
47+
}
48+
}
49+
50+
}

0 commit comments

Comments
 (0)