import java.util.HashMap; import java.util.Map; /** * ============================================ * 任务一:泛型类 Pair * - 包含构造方法、getter/setter * - swap() 方法用于交换 K 和 V 的值 * ============================================ */ class Pair { private K key; private V value; public Pair() { } 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; } /** * 交换 key 和 value 的值 */ public void swap() { K temp = this.key; this.key = (K) this.value; this.value = (V) temp; } @Override public String toString() { return "Pair{key=" + key + ", value=" + value + "}"; } public static void main(String[] args) { System.out.println("========== Pair 测试 =========="); Pair pair = new Pair<>("Hello", 100); System.out.println("交换前: " + pair); pair.swap(); System.out.println("交换后: " + pair); Pair pair2 = new Pair<>(3.14, "PI"); System.out.println("交换前: " + pair2); pair2.swap(); System.out.println("交换后: " + pair2); } } /** * ============================================ * 任务二:泛型缓存类 Cache * - put(K key, V value): 添加缓存 * - get(K key): 获取缓存 * - remove(K key): 删除缓存 * - clear(): 清空缓存 * ============================================ */ class Cache { private final Map cache; public Cache() { this.cache = new HashMap<>(); } public void put(K key, V value) { if (key == null) { throw new IllegalArgumentException("key不能为null"); } cache.put(key, value); } public V get(K key) { if (key == null) { return null; } return cache.get(key); } public V remove(K key) { if (key == null) { return null; } return cache.remove(key); } public void clear() { cache.clear(); } public int size() { return cache.size(); } public boolean containsKey(K key) { if (key == null) { return false; } return cache.containsKey(key); } @Override public String toString() { return "Cache{" + cache + "}"; } public static void main(String[] args) { System.out.println("========== Cache 测试 =========="); Cache cache = new Cache<>(); cache.put("Java", 90); cache.put("Python", 85); cache.put("C++", 92); System.out.println("添加后缓存: " + cache); System.out.println("缓存大小: " + cache.size()); System.out.println("获取Java成绩: " + cache.get("Java")); System.out.println("获取不存在的Key: " + cache.get("Go")); cache.put("Java", 95); System.out.println("更新Java成绩后: " + cache); System.out.println("删除Python: " + cache.remove("Python")); System.out.println("删除后缓存: " + cache); cache.clear(); System.out.println("清空后缓存: " + cache); try { cache.put(null, 100); } catch (IllegalArgumentException e) { System.out.println("null key测试: " + e.getMessage()); } } } /** * ============================================ * 主类:运行测试 * ============================================ */ public class GenericHomework { public static void main(String[] args) { System.out.println("【任务一】Pair 测试"); System.out.println("========================"); Pair.main(null); System.out.println(); System.out.println("【任务二】Cache 测试"); System.out.println("========================"); Cache.main(null); } }