본문 바로가기

기존카테고리/JAVA

GenericDemo

package com.test.ex;


class EmployeeInfo{

public String rank;

EmployeeInfo(String rank){

this.rank=rank;

}

}

class StudentInfo{

public int grade;

StudentInfo(int grade){

this.grade=grade;

}

}


class Person<T, S>{

public T info;

public S id;

Person(T info, S id){

this.info=info;

this.id=id;

}

public <U> void printInfo(U info){

System.out.println(info);

}

}


public class GenericDemo {


public static void main(String[] args) {

EmployeeInfo e = new EmployeeInfo("lee");

Integer i = new Integer(21);

Person<EmployeeInfo,Integer> p1 = new Person<EmployeeInfo,Integer>(e,i);

//Person p1 = new Person(e,i);

System.out.println(p1.info.rank);

System.out.println(p1.id.intValue());

StudentInfo a = new StudentInfo(55);

Person<StudentInfo,Integer> p2 = new Person<StudentInfo,Integer>(a, i);

System.out.println(p2.info.grade);

p1.<Integer>printInfo(a.grade);

}


}



'기존카테고리 > JAVA' 카테고리의 다른 글

상속  (0) 2020.03.29
객체직렬화(Serializable) 사용방법 & 이유(펌)  (0) 2017.06.26