Ex01_generic
package com.mywork.ex;
class Box{
private Object obj;
public void setObj(Object obj) {
this.obj = obj;
}
public Object getObj() {
return obj;
}
}
class BallPen{ }
class Pencil{ }
public class Ex01_generic {
public static void main(String[] args) {
Box box = new Box();
box.setObj(new BallPen());
BallPen myPen = (BallPen)box.getObj();
System.out.println(myPen);
box.setObj(new Pencil());
Pencil myPencil = (Pencil)box.getObj();
System.out.println(myPencil);
}
}
Ex02_generic
package com.mywork.ex;
class Bag <T>{
private T obj;
public void setObj(T obj) {
this.obj = obj;
}
public T getObj() {
return obj;
}
}
class Ball{ }
class Pen{ }
public class Ex02_generic {
public static void main(String[] args) {
Bag<Ball> bag = new Bag<>();
bag.setObj(new Ball());
Ball ball = bag.getObj();
System.out.println(ball);
}
}
Ex03_generic
package com.mywork.ex;
public class Ex03_generic {
static <T> void printArray(T[] arr) {
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
System.out.println();
}
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 4, 5};
printArray(arr);
Double[] arr2 = {1.5, 2.5, 3.5};
printArray(arr2);
String[] arr3 = {"hello", "sidney"};
printArray(arr3);
}
}
Ex04_generic
package com.mywork.ex;
class Room <T1, T2>{
private T1 furniture1;
private T2 furniture2;
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("첫 번째 가구 : ").append(furniture1).append("\n");
sb.append("두 번째 가구 : ").append(furniture2);
return sb.toString();
}
public void setFurniture1(T1 furniture1) {
this.furniture1 = furniture1;
}
public void setFurniture2(T2 furniture2) {
this.furniture2 = furniture2;
}
}
class Bed{
@Override
public String toString() {
return "침대";
}
}
class DressTable{
@Override
public String toString() {
return "옷장";
}
}
class Chair{
@Override
public String toString() {
return "의자";
}
}
class Table{
@Override
public String toString() {
return "책상";
}
}
public class Ex04_generic {
public static void main(String[] args) {
Room<Bed, DressTable> mainRoom = new Room<>();
Room<Chair, Table> studyRoom = new Room<>();
mainRoom.setFurniture1(new Bed());
mainRoom.setFurniture2(new DressTable());
studyRoom.setFurniture1(new Chair());
studyRoom.setFurniture2(new Table());
System.out.println(mainRoom);
System.out.println(studyRoom);
}
}
Ex05_generic
package com.mywork.ex;
import java.util.Arrays;
class Container<T>{
private T[] items;
@SuppressWarnings("unchecked")
public Container(int capacity) {
items = (T[])(new Object[capacity]);
}
public void add(T item) {
for(int i = 0; i < items.length; i++) {
if(items[i] == null) {
items[i] = item;
break;
}
}
}
public T[] getItems() {
return items;
}
}
class Gun{
private String model;
public Gun(String model) {
this.model = model;
}
@Override
public String toString() {
return model;
}
}
public class Ex05_generic {
public static void main(String[] args) {
Container<Gun> container = new Container<>(10);
container.add(new Gun("K1"));
container.add(new Gun("K9"));
container.add(new Gun("K3"));
System.out.println(Arrays.toString(container.getItems()));
}
}
Ex06_generic
package com.mywork.ex;
class Basket <T>{
private T[] foods;
@SuppressWarnings("unchecked")
public Basket(int length) {
foods = (T[])(new Object[length]);
}
public void add(T food) {
for(int i = 0; i < foods.length; i++) {
if(foods[i] == null) {
foods[i] = food;
break;
}
}
}
}
class Food { }
class Apple extends Food{ }
class Banana extends Food{ }
class Bread extends Food{ }
class Computer{ }
public class Ex06_generic {
public static void main(String[] args) {
Basket<Food> basket = new Basket<>(5);
basket.add(new Apple());
basket.add(new Banana());
basket.add(new Bread());
}
}