class EmployeeInfo4{
public int rank;
EmployeeInfo4( int rank) { this .rank = rank; }
}
class Person4< T, S> { //generic으로 원시 기본 data type 을 사용할 수 없다. 사용하고 싶다면 wrapper class 을 사용해야한다. 원시 기본 data type을 객체화 시키는것
public T info;
public S id;
Person4( T info, S id) {
this .info = info;
this .id = id;
}
public < U> void printInfo ( U info) {
}
}
public class GenericDemo4 {
public static void main
( String [ ] args
) { //Integer => int 에 대한 wrapping class
System .
out .
println ( "Multiple Generic Demo\n " ) ;
EmployeeInfo4 e = new EmployeeInfo4( 1 ) ;
Person4< EmployeeInfo4, /*int*/ Integer> p1 = new Person4< EmployeeInfo4, /*int*/ Integer> ( e, i) ;
Person4 p2 = new Person4( e, i) ;
//Person4 p1 = new Person4(new EmployeeInfo4(1), new Integer(10));
//p1
System .
out .
println ( p1.
info .
rank ) ; System .
out .
println ( p1.
id .
intValue ( ) ) ;
p1.< Integer> printInfo( e.rank ) ;
p1.printInfo ( e.rank ) ;
p1.< Integer> printInfo( i) ;
p1.printInfo ( i) ;
p1.< Integer> printInfo( i.intValue ( ) ) ;
p1.printInfo ( i.intValue ( ) ) ;
//p2
System .
out .
println ( p2.
info .
rank ) ; //=>오류 why? System .
out .
println ( p2.
id .
intValue ( ) ) ; //=>오류 why?
p2.< Integer> printInfo( e.rank ) ;
p2.printInfo ( e.rank ) ;
p2.< Integer> printInfo( i) ;
p2.printInfo ( i) ;
p2.< Integer> printInfo( i.intValue ( ) ) ;
p2.printInfo ( i.intValue ( ) ) ;
}
}
CgpjbGFzcyBFbXBsb3llZUluZm80ewogICAgcHVibGljIGludCByYW5rOwogICAgRW1wbG95ZWVJbmZvNChpbnQgcmFuayl7IHRoaXMucmFuayA9IHJhbms7IH0KfQpjbGFzcyBQZXJzb240PFQsIFM+eyAvL2dlbmVyaWPsnLzroZwg7JuQ7IucIOq4sOuzuCBkYXRhIHR5cGUg7J2EIOyCrOyaqe2VoCDsiJgg7JeG64ukLiDsgqzsmqntlZjqs6Ag7Iu264uk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nByaW50bG4ocDEuaW5mby5yYW5rKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocDEuaWQpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihwMS5pZC5pbnRWYWx1ZSgpKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oImVuZFxuIik7CiAgICAgICAgCiAgICAgICAgcDEuPEludGVnZXI+cHJpbnRJbmZvKGUucmFuayk7CiAgICAgICAgcDEucHJpbnRJbmZvKGUucmFuayk7CiAgICAgICAgcDEuPEludGVnZXI+cHJpbnRJbmZvKGkpOwogICAgICAgIHAxLnByaW50SW5mbyhpKTsKICAgICAgICBwMS48SW50ZWdlcj5wcmludEluZm8oaS5pbnRWYWx1ZSgpKTsKICAgICAgICBwMS5wcmludEluZm8oaS5pbnRWYWx1ZSgpKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oImVuZFxuIik7CiAgICAgICAgCi8vcDIgICAgICAgIAogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihwMi5pbmZvLnJhbmspOyAvLz0+7Jik66WYIHdoeT8KICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocDIuaWQpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihwMi5pZC5pbnRWYWx1ZSgpKTsgLy89PuyYpOulmCB3aHk/CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJlbmRcbiIpOwogICAgICAgIAogICAgICAgIHAyLjxJbnRlZ2VyPnByaW50SW5mbyhlLnJhbmspOwogICAgICAgIHAyLnByaW50SW5mbyhlLnJhbmspOwogICAgICAgIHAyLjxJbnRlZ2VyPnByaW50SW5mbyhpKTsKICAgICAgICBwMi5wcmludEluZm8oaSk7CiAgICAgICAgcDIuPEludGVnZXI+cHJpbnRJbmZvKGkuaW50VmFsdWUoKSk7CiAgICAgICAgcDIucHJpbnRJbmZvKGkuaW50VmFsdWUoKSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJlbmRcbiIpOwogICAgICAgIAogICAgfQp9
compilation info
Main.java:18: error: class GenericDemo4 is public, should be declared in a file named GenericDemo4.java
public class GenericDemo4 {
^
Main.java:45: error: cannot find symbol
System.out.println(p2.info.rank); //=>?? why?
^
symbol: variable rank
location: variable info of type T
where T is a type-variable:
T extends Object declared in class Person4
Main.java:47: error: cannot find symbol
System.out.println(p2.id.intValue()); //=>?? why?
^
symbol: method intValue()
location: variable id of type S
where S is a type-variable:
S extends Object declared in class Person4
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
stdout