Binary World

JAVA CLASS : Generic 클래스 본문

개발자의 길/JAVA

JAVA CLASS : Generic 클래스

모쿠 2017. 1. 6. 17:15

<Generic 클래스>

* 여러가지 변수, 메소드 등을 받아서 저장할 수 있는 클래스

* 두 개 이상의 일반화 변수를 갖는 generic 클래스


<프로그램 코드>


* GenericMain02.java


package edu.java.generic02;


class Test<T, U>{

private T item1;

private U item2;

public Test(T item1, U item2){

this.item1 = item1;

this.item2 = item2;

}

public void display() {

System.out.println("아이템1 : " + item1);

System.out.println("아이템2 : " + item2);

}

} // end class Test


public class GenericMain02 {


public static void main(String[] args) {

Test<Integer, Integer> test1 = new Test<>(111, 222);

test1.display();

System.out.println();

Test<Integer, String> test2 = new Test<>(111, "Hello");

test2.display();


} // end main()


} // end class GenericMain02

 



* 출력화면


아이템1 : 111

아이템2 : 222


아이템1 : 111

아이템2 : Hello




Comments