fork(1) download
  1.  
  2.  
  3. class EmployeeInfo4{
  4. public int rank;
  5. EmployeeInfo4(int rank){ this.rank = rank; }
  6. }
  7. class Person4<T, S>{ //generic으로 원시 기본 data type 을 사용할 수 없다. 사용하고 싶다면 wrapper class 을 사용해야한다. 원시 기본 data type을 객체화 시키는것
  8. public T info;
  9. public S id;
  10. Person4(T info, S id){
  11. this.info = info;
  12. this.id = id;
  13. }
  14. public <U> void printInfo (U info) {
  15. System.out.println(info);
  16. }
  17. }
  18. public class GenericDemo4 {
  19. public static void main(String[] args) { //Integer => int 에 대한 wrapping class
  20.  
  21. System.out.println("");
  22. System.out.println("Multiple Generic Demo\n");
  23.  
  24. EmployeeInfo4 e = new EmployeeInfo4(1);
  25. Integer i = new Integer(10);
  26. Person4<EmployeeInfo4, /*int*/Integer> p1 = new Person4<EmployeeInfo4, /*int*/Integer>(e, i);
  27. Person4 p2 = new Person4(e, i);
  28. //Person4 p1 = new Person4(new EmployeeInfo4(1), new Integer(10));
  29.  
  30. //p1
  31. System.out.println(p1.info.rank);
  32. System.out.println(p1.id);
  33. System.out.println(p1.id.intValue());
  34. System.out.println("end\n");
  35.  
  36. p1.<Integer>printInfo(e.rank);
  37. p1.printInfo(e.rank);
  38. p1.<Integer>printInfo(i);
  39. p1.printInfo(i);
  40. p1.<Integer>printInfo(i.intValue());
  41. p1.printInfo(i.intValue());
  42. System.out.println("end\n");
  43.  
  44. //p2
  45. System.out.println(p2.info.rank); //=>오류 why?
  46. System.out.println(p2.id);
  47. System.out.println(p2.id.intValue()); //=>오류 why?
  48. System.out.println("end\n");
  49.  
  50. p2.<Integer>printInfo(e.rank);
  51. p2.printInfo(e.rank);
  52. p2.<Integer>printInfo(i);
  53. p2.printInfo(i);
  54. p2.<Integer>printInfo(i.intValue());
  55. p2.printInfo(i.intValue());
  56. System.out.println("end\n");
  57.  
  58. }
  59. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
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
Standard output is empty