Sponsored By
















728x90

 

 

제네릭(Generic, 포장)클래스 에 대한 사용방법임.

 

 

 

 

제네릭클래스를 사용하는 이유는 같은 메소드의 변수타입만 바꿔주고 싶을때 주로 사용하게 된다.

 

사용방법은 객체를 생성할때 객체이름<타입> 을 적어주면 된다.

 

 

 

 

 

00. MyStack<T> 라고 둬서 바꿔주고 싶은 곳에 <T>를 둔다.

     객체를 생성할때 MySrack<Integer> ms1 = new MyStack<Integer>(); 로 생성하면 int 타입으로 생성할 수 있다.

 

 

 

 

 

※. 코드

 

 

class Stack{

 

private int[] stack;

private int top;

 

public void push(int value)

{

 

}

 

}// Stack Class

 

class MyStack <T> {

 

private T[] stack;

private int top;

 

public void push(T value)

{

 

}

 

}// MyStack Class

 

 

 

public class Test {

 

public static void main(String[] args) {

 

Stack s1 = new Stack();

MyStack<String> ms1 = new MyStack<String>();

MyStack<Integer> ms2 = new MyStack<Integer>();

 

 

}// main

 

}// Main Class

 

 

 

 

728x90


Sponsored By















+ Recent posts