당연히 array도 가능하겠죠?
public class Utility {
// 제네릭 메서드
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.println(element);
}
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
String[] stringArray = {"Hello", "World"};
// 제네릭 메서드 호출
Utility.<Integer>printArray(intArray);
Utility.<String>printArray(stringArray);
}
}
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;
}
@Override
public String toString() {
return "Pair{" +
"key=" + key +
", value=" + value +
'}';
}
public static void main(String[] args) {
// String과 Integer 타입을 사용하는 Pair 객체
Pair<String, Integer> pair1 = new Pair<String, Integer>("One", 1);
System.out.println(pair1);
// String과 String 타입을 사용하는 Pair 객체
Pair<String, String> pair2 = new Pair<String, String>("Hello", "World");
System.out.println(pair2);
// Integer와 Double 타입을 사용하는 Pair 객체
Pair<Integer, Double> pair3 = new Pair<Integer, Double>(42, 3.14);
System.out.println(pair3);
}
}
public class MyClass<T extends Comparable> {
// 이 코드는 T가 Comparable 타입만 가능하도록 강제합니다.
}
public class MyClass<T extends Employee & Comparable & Iterable> {
// 이 코드는 T가 Employee의 서브클래스이면서 동시에 Comparable 및 Iterable 인터페이스를 구현해야 한다는 것을 강제합니다.
// 확장된 클래스(Employee)는 항상 첫 번째로 나와야 합니다.
}