public class 클래스명<T> { ... }
public interface 인터페이스명<T> { ... }
public class Box<T> {
public T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
public class Main {
public static void main(String[] args) {
Box<String> stringBox = new Box<>();
stringBox.setT("문자열");
String stringBoxT = stringBox.getT();
System.out.println("stringBoxT = " + stringBoxT); // 저장된 문자열 출력
Box<Integer> integerBox = new Box<>();
integerBox.setT(100);
Integer integerBoxT = integerBox.getT();
System.out.println("integerBoxT = " + integerBoxT); // 저장된 숫자 출력
}
}
public class Product <M, P>{
private M model;
private P price;
public M getModel() {
return model;
}
public void setModel(M model) {
this.model = model;
}
public P getPrice() {
return price;
}
public void setPrice(P price) {
this.price = price;
}
}
Product<String, Integer> product1 = new Product<>();
product1.setModel("K-11");
product1.setPrice(10000);
public class Laptop {
String cpu;
String ram;
String vga;
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public String getRam() {
return ram;
}
public void setRam(String ram) {
this.ram = ram;
}
public String getVga() {
return vga;
}
public void setVga(String vga) {
this.vga = vga;
}
@Override
public String toString() {
return "Laptop{" +
"cpu='" + cpu + '\'' +
", ram='" + ram + '\'' +
", vga='" + vga + '\'' +
'}';
}
}
public class DiscountablePrice {
double discountRate;
double price;
public double getDiscountRate() {
return discountRate;
}
public void setDiscountRate(double discountRate) {
this.discountRate = discountRate;
}
public double getPrice() {
return price - (price * discountRate);
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "DiscountablePrice{" +
"discountRate=" + discountRate +
", price=" + price +
", discountedPrice=" + this.getPrice() +
'}';
}
}
public class Main {
public static void main(String[] args) {
Product<Laptop, DiscountablePrice> product2 = new Product<>();
Laptop laptopModel = new Laptop();
laptopModel.setCpu("i7");
laptopModel.setRam("16gb");
laptopModel.setVga("Geforce 1070");
product2.setModel(laptopModel);
DiscountablePrice discountablePrice = new DiscountablePrice();
discountablePrice.setPrice(10000);
discountablePrice.setDiscountRate(0.2);
product2.setPrice(discountablePrice);
System.out.println(product2.getModel() + " + " + product2.getPrice());
}
}
public static <P, R> R genericTypeMethod(P param, R paramToReturn) {
System.out.println("param = " + param);
System.out.println("paramToReturn = " + paramToReturn);
return paramToReturn;
}
public static void main(String[] args) {
ParamType param = new ParamType();
param.setParamString("파라미터");
ReturnType paramToReturn = new ReturnType();
paramToReturn.setReturnString("리턴");
genericTypeMethod(param, paramToReturn);
}
public class Pair <K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
public class Util {
public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
boolean keyCompare = p1.getKey().equals(p2.getKey());
boolean valueCompare = p1.getValue().equals(p2.getValue());
return keyCompare && valueCompare;
}
}
public class Main {
public static void main(String[] args) {
Pair<String, String> p1 = new Pair<>("1", "홍길동");
Pair<String, String> p2 = new Pair<>("1", "홍길동");
boolean compare1 = Util.compare(p1, p2);
if(compare1) {
System.out.println("key와 value가 모두 일치합니다.");
} else {
System.out.println("key와 value가 일치하지 않습니다.");
}
Pair<Integer, String> p3 = new Pair<>(100, "user1");
Pair<Integer, String> p4 = new Pair<>(100, "user2");
boolean compare2 = Util.compare(p3, p4);
if (compare2) {
System.out.println("key와 value가 모두 일치합니다.");
} else {
System.out.println("key와 value가 일치하지 않습니다.");
}
}
}
public class NumberUtil {
public static <T extends Number> double add(T num1, T num2) {
return num1.doubleValue() + num2.doubleValue();
}
}
public class Main {
public static void main(String[] args) {
int a = 10;
double b = 20.10;
double result = NumberUtil.add(a, b);
System.out.println("result = " + result); // 결과 30.1
}
}
public class ChildProduct<M, P, C> extends Product<M, P> {
private C company;
public C getCompany() {
return company;
}
public void setCompany(C company) {
this.company = company;
}
}