diff --git a/w8/Cache.java b/w8/Cache.java new file mode 100644 index 0000000..0ca78fb --- /dev/null +++ b/w8/Cache.java @@ -0,0 +1,83 @@ +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; + +public class Cache { + private final int capacity; + private final Map cache; + + public Cache(int capacity) { + this.capacity = capacity; + this.cache = new HashMap<>(); + } + + public void put(K key, V value) { + if (key == null) { + return; + } + if (cache.size() >= capacity && !cache.containsKey(key)) { + K oldestKey = cache.keySet().iterator().next(); + cache.remove(oldestKey); + } + cache.put(key, value); + } + + public V get(K key) { + return cache.get(key); + } + + public void remove(K key) { + cache.remove(key); + } + + public void clear() { + cache.clear(); + } + + public int getCapacity() { + return capacity; + } + + public int getSize() { + return cache.size(); + } + + public boolean containsKey(K key) { + return cache.containsKey(key); + } + + @SuppressWarnings("unchecked") + public Class getKeyType() { + Type type = getClass().getGenericSuperclass(); + if (type instanceof ParameterizedType) { + ParameterizedType paramType = (ParameterizedType) type; + Type[] typeArgs = paramType.getActualTypeArguments(); + if (typeArgs != null && typeArgs.length > 0 && typeArgs[0] instanceof Class) { + return (Class) typeArgs[0]; + } + } + return null; + } + + @SuppressWarnings("unchecked") + public Class getValueType() { + Type type = getClass().getGenericSuperclass(); + if (type instanceof ParameterizedType) { + ParameterizedType paramType = (ParameterizedType) type; + Type[] typeArgs = paramType.getActualTypeArguments(); + if (typeArgs != null && typeArgs.length > 1 && typeArgs[1] instanceof Class) { + return (Class) typeArgs[1]; + } + } + return null; + } + + @Override + public String toString() { + return "Cache{" + + "capacity=" + capacity + + ", cache=" + cache + + '}'; + } +} \ No newline at end of file diff --git a/w8/CacheTest.java b/w8/CacheTest.java new file mode 100644 index 0000000..5d86e03 --- /dev/null +++ b/w8/CacheTest.java @@ -0,0 +1,33 @@ +public class CacheTest { + public static void main(String[] args) { + System.out.println("========== Cache 测试 =========="); + + Cache cache1 = new Cache<>(3); + cache1.put("name", "Alice"); + cache1.put("age", "20"); + cache1.put("city", "Beijing"); + System.out.println("Cache(容量3)添加3个元素: " + cache1); + + cache1.put("country", "China"); + System.out.println("Cache(容量3)添加第4个元素: " + cache1); + + System.out.println("获取name: " + cache1.get("name")); + System.out.println("删除age后: "); + cache1.remove("age"); + System.out.println(cache1); + + Cache cache2 = new Cache<>(2); + cache2.put("a", 1); + cache2.put("b", 2); + cache2.put("c", 3); + System.out.println("\nCache(容量2)添加3个元素(触发LRU淘汰): " + cache2); + + Cache cache3 = new Cache<>(4); + cache3.put(1, 1.1); + cache3.put(2, 2.2); + System.out.println("\nCache(容量4)添加2个元素: " + cache3); + + System.out.println("\n测试包含键: " + cache3.containsKey(1)); + System.out.println("测试不包含键: " + cache3.containsKey(5)); + } +} \ No newline at end of file diff --git a/w8/GenericReflection.java b/w8/GenericReflection.java new file mode 100644 index 0000000..709c193 --- /dev/null +++ b/w8/GenericReflection.java @@ -0,0 +1,54 @@ +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Map; + +public class GenericReflection { + public static void main(String[] args) { + System.out.println("========== Java泛型擦除后通过反射获取泛型信息 ==========\n"); + + System.out.println("1. 获取字段的泛型类型:"); + try { + Field mapField = Cache.class.getDeclaredField("cache"); + System.out.println(" 字段名: " + mapField.getName()); + System.out.println(" 原始类型: " + mapField.getType()); + + Type genericType = mapField.getGenericType(); + if (genericType instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) genericType; + System.out.println(" 参数化类型: " + pt.getTypeName()); + System.out.println(" 泛型参数[0]: " + pt.getActualTypeArguments()[0]); + System.out.println(" 泛型参数[1]: " + pt.getActualTypeArguments()[1]); + } + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + System.out.println("\n2. 获取子类的泛型类型(通过ParameterizedType):"); + StringCache cache = new StringCache(); + cache.put("test", "value"); + + Type superType = cache.getClass().getGenericSuperclass(); + if (superType instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) superType; + System.out.println(" 父类类型: " + pt.getTypeName()); + System.out.println(" K类型: " + pt.getActualTypeArguments()[0]); + System.out.println(" V类型: " + pt.getActualTypeArguments()[1]); + } + + System.out.println("\n3. 总结:"); + System.out.println(" - 泛型信息在编译时被擦除到上限类型(Object或指定上限)"); + System.out.println(" - 通过反射可以在运行时获取以下泛型信息:"); + System.out.println(" * Field.getGenericType() 获取字段的泛型类型"); + System.out.println(" * Method.getGenericParameterTypes() 获取方法参数的泛型类型"); + System.out.println(" * Method.getGenericReturnType() 获取方法返回值的泛型类型"); + System.out.println(" * Class.getGenericSuperclass() 获取父类的泛型类型"); + System.out.println(" - 子类继承泛型类时,泛型信息会保留在子类的Class对象中"); + } +} + +class StringCache extends Cache { + public StringCache() { + super(10); + } +} \ No newline at end of file diff --git a/w8/Pair.java b/w8/Pair.java new file mode 100644 index 0000000..3c1b7ea --- /dev/null +++ b/w8/Pair.java @@ -0,0 +1,37 @@ +public class Pair { + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + public void setKey(K key) { + this.key = key; + } + + public void setValue(V value) { + this.value = value; + } + + public Pair swap() { + return new Pair<>(this.value, this.key); + } + + @Override + public String toString() { + return "Pair{" + + "key=" + key + + ", value=" + value + + '}'; + } +} \ No newline at end of file diff --git a/w8/PairTest.java b/w8/PairTest.java new file mode 100644 index 0000000..e3148b1 --- /dev/null +++ b/w8/PairTest.java @@ -0,0 +1,20 @@ +public class PairTest { + public static void main(String[] args) { + System.out.println("========== Pair 测试 =========="); + + Pair pair1 = new Pair<>("apple", 1); + System.out.println("原始 Pair: " + pair1); + + Pair swappedPair = pair1.swap(); + System.out.println("交换后 Pair: " + swappedPair); + + Pair pair2 = new Pair<>(3.14, "PI"); + System.out.println("原始 Pair: " + pair2); + + Pair swappedPair2 = pair2.swap(); + System.out.println("交换后 Pair: " + swappedPair2); + + System.out.println("\n交换方法的键值类型: " + + "Pair -> Pair 成功!"); + } +} \ No newline at end of file