본문 바로가기

JAVA/CollectionFramework

generic 예제

MainWrapper

package ex01_generic;

public class MainWrapper {

	public static void main(String[] args) {

		//String을 저장하는 box1
		Box<String>box1=new Box<String>();
		box1.setItem("Hello world");
		System.out.println(box1.getItem());
		
		
		//Integer 를 저장하는 box2
		Box<Integer>box2=new Box<Integer>();
		box2.setItem(3);
		System.out.println(box2.getItem());
		
		//Person을 저장하는 box3
		Box<Person>box3=new Box<Person>();
		box3.setItem(new Person("김응가"));
		System.out.println(box3.getItem());
		
		Dog<DogKind>dog1=new Dog<DogKind>();
		dog1.setKind(new DogKind("진돚개"));
		System.out.println(dog1.getKind());
		
		
	}

}

 

 

 

 

Box

package ex01_generic;

public class Box<T>{

	private T item;

	public T getItem() {
		return item;
	}

	public void setItem(T item) {
		this.item = item;
	}
	
	
}

 

 

 

 

Person

package ex01_generic;

public class Person {

	private String name;

	@Override
	public String toString() {
		return "Person [이름=" + name + "]";
	}

	public Person(String name)
	{
		this.name=name;
	}
	
}

 

 

 

Dog

package ex01_generic;

public class Dog<D>{

	private D Kind;

	public D getKind() {
		return Kind;
	}

	public void setKind(D kind) {
		Kind = kind;
	}
	

}

 

 

DogKind

package ex01_generic;

public class DogKind {

	private String kind;
	

	@Override
	public String toString() {
		return "개품종=" + kind  ;
	}


	public DogKind(String kind)
	{
		this.kind=kind;
	}


}

 

출력결과

 

벨로그주소:https://velog.io/@000000hj

 

000000hj (이현주) - velog

 

velog.io

깃허브 주소

https://github.com/000000hj/MyPractice/tree/main/11_CollectionFramework

 

'JAVA > CollectionFramework' 카테고리의 다른 글

List  (0) 2023.07.29