From b05be572a8ec910b7d4c5cd7c1a1351ce62178c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SALAH=20ABDULLAH=E9=98=BF=E5=B1=B1?= <2040abdullah@gmail.com> Date: Sun, 31 May 2026 03:28:23 +0800 Subject: [PATCH] w55 --- src/W88/Cache.java | 0 src/W88/GenericReflectionExample.java | 0 src/W88/Main.java | 0 src/W88/Pair.java | 0 src/w6/Main.java | 50 ----------------- src/w7/Main.java | 51 ----------------- src/w8/Main.java | 40 ------------- src/w8/W88/Cache.java | 0 src/w8/W88/GenericReflectionExample.java | 0 src/w8/W88/Main.java | 0 src/w8/W88/Pair.java | 0 src/w8/W88/W88/Cache.java | 59 ++++++++++++++++++++ src/w8/W88/W88/GenericReflectionExample.java | 53 ++++++++++++++++++ src/w8/W88/W88/Main.java | 33 +++++++++++ src/w8/W88/W88/Pair.java | 36 ++++++++++++ 15 files changed, 181 insertions(+), 141 deletions(-) create mode 100644 src/W88/Cache.java create mode 100644 src/W88/GenericReflectionExample.java create mode 100644 src/W88/Main.java create mode 100644 src/W88/Pair.java delete mode 100644 src/w6/Main.java delete mode 100644 src/w7/Main.java delete mode 100644 src/w8/Main.java create mode 100644 src/w8/W88/Cache.java create mode 100644 src/w8/W88/GenericReflectionExample.java create mode 100644 src/w8/W88/Main.java create mode 100644 src/w8/W88/Pair.java create mode 100644 src/w8/W88/W88/Cache.java create mode 100644 src/w8/W88/W88/GenericReflectionExample.java create mode 100644 src/w8/W88/W88/Main.java create mode 100644 src/w8/W88/W88/Pair.java diff --git a/src/W88/Cache.java b/src/W88/Cache.java new file mode 100644 index 0000000..e69de29 diff --git a/src/W88/GenericReflectionExample.java b/src/W88/GenericReflectionExample.java new file mode 100644 index 0000000..e69de29 diff --git a/src/W88/Main.java b/src/W88/Main.java new file mode 100644 index 0000000..e69de29 diff --git a/src/W88/Pair.java b/src/W88/Pair.java new file mode 100644 index 0000000..e69de29 diff --git a/src/w6/Main.java b/src/w6/Main.java deleted file mode 100644 index 6531a33..0000000 --- a/src/w6/Main.java +++ /dev/null @@ -1,50 +0,0 @@ -package w6; - -// 游泳接口 -interface Swimmable { - void swim(); -} - -// 抽象动物类 -abstract class Animal { - public abstract void makeSound(); -} - -// 狗:继承Animal + 实现游泳接口 -class Dog extends Animal implements Swimmable { - @Override - public void makeSound() { - System.out.println("小狗汪汪汪叫"); - } - - @Override - public void swim() { - System.out.println("小狗会游泳,正在游泳"); - } -} - -// 猫:只继承Animal,不实现游泳接口 -class Cat extends Animal { - @Override - public void makeSound() { - System.out.println("小猫喵喵喵叫"); - } -} - -// 主类名改成Main!和文件名Main.java完全一致 -public class Main { - public static void main(String[] args) { - // 多态测试:父类引用指向子类对象 - Animal animal1 = new Dog(); - Animal animal2 = new Cat(); - - // 多态调用叫声 - animal1.makeSound(); - animal2.makeSound(); - - // 向下转型调用游泳方法 - if (animal1 instanceof Dog dog) { - dog.swim(); - } - } -} diff --git a/src/w7/Main.java b/src/w7/Main.java deleted file mode 100644 index 77e3246..0000000 --- a/src/w7/Main.java +++ /dev/null @@ -1,51 +0,0 @@ -package w7; -// Animal.java - 动物抽象类 -abstract class Animal { - public abstract void makeSound(); // 抽象方法:发出声音 -} - -// Swimable.java - 游泳接口 -interface Swimable { - void swim(); // 游泳方法 -} - -// Dog.java - 狗类继承动物并实现游泳接口 -class Dog extends Animal implements Swimable { - @Override - public void makeSound() { - System.out.println("狗叫:汪汪!"); - } - - @Override - public void swim() { - System.out.println("狗在游泳。"); - } -} - -// Cat.java - 猫类只继承动物,不实现游泳接口 -class Cat extends Animal { - @Override - public void makeSound() { - System.out.println("猫叫:喵喵!"); - } -} - -// Main.java - 测试多态调用 -public class Main { - public static void main(String[] args) { - // 多态:父类引用指向子类对象 - Animal myDog = new Dog(); - Animal myCat = new Cat(); - - // 调用 makeSound() - 展示不同动物的不同行为 - myDog.makeSound(); // 狗叫 - myCat.makeSound(); // 猫叫 - - // 测试游泳:只有狗能游泳(使用 instanceof 检查类型) - if (myDog instanceof Swimable) { - ((Swimable) myDog).swim(); // 狗游泳 - } - - // 猫不能游泳(Cat 没有实现 Swimable 接口) - } -} \ No newline at end of file diff --git a/src/w8/Main.java b/src/w8/Main.java deleted file mode 100644 index 81b580f..0000000 --- a/src/w8/Main.java +++ /dev/null @@ -1,40 +0,0 @@ -package w8; - public class Main { - public static void main(String[] args) throws InterruptedException { - // Pair test - Pair pair = new Pair<>("age", 25); - System.out.println("Original: " + pair); - System.out.println("Swap: " + pair.swap()); - - // Cache test - Cache cache = new Cache<>(3000); - cache.put("name", "Ahmed"); - System.out.println("Cache get: " + cache.get("name")); - } -} - -class Pair { - private K key; - private V value; - public Pair(K key, V value) { this.key = key; this.value = value; } - public Pair swap() { return new Pair<>(value, key); } - public String toString() { return "Pair{" + key + "," + value + "}"; } -} - -class Cache { - private java.util.HashMap> cache = new java.util.HashMap<>(); - private long defaultExpireTime; - public Cache(long t) { this.defaultExpireTime = t; } - public void put(K key, V value) { - cache.put(key, new CacheEntry<>(value, defaultExpireTime > 0 ? System.currentTimeMillis() + defaultExpireTime : 0)); - } - public V get(K key) { - CacheEntry entry = cache.get(key); - if (entry == null || (entry.expireTime > 0 && System.currentTimeMillis() > entry.expireTime)) return null; - return entry.value; - } - private static class CacheEntry { - V value; long expireTime; - CacheEntry(V v, long e) { value = v; expireTime = e; } - } -} \ No newline at end of file diff --git a/src/w8/W88/Cache.java b/src/w8/W88/Cache.java new file mode 100644 index 0000000..e69de29 diff --git a/src/w8/W88/GenericReflectionExample.java b/src/w8/W88/GenericReflectionExample.java new file mode 100644 index 0000000..e69de29 diff --git a/src/w8/W88/Main.java b/src/w8/W88/Main.java new file mode 100644 index 0000000..e69de29 diff --git a/src/w8/W88/Pair.java b/src/w8/W88/Pair.java new file mode 100644 index 0000000..e69de29 diff --git a/src/w8/W88/W88/Cache.java b/src/w8/W88/W88/Cache.java new file mode 100644 index 0000000..fbbae2e --- /dev/null +++ b/src/w8/W88/W88/Cache.java @@ -0,0 +1,59 @@ +package w8.W88.W88; + +import java.util.concurrent.ConcurrentHashMap; + +public class Cache { + private ConcurrentHashMap cache; + private long defaultTTL; + + public Cache() { + this.cache = new ConcurrentHashMap<>(); + this.defaultTTL = 60000; + } + + public Cache(long ttlMillis) { + this.cache = new ConcurrentHashMap<>(); + this.defaultTTL = ttlMillis; + } + + public void put(K key, V value) { + if (key == null) { + System.out.println("Error: Key cannot be null"); + return; + } + cache.put(key, value); + System.out.println("Cache put: " + key + " = " + value); + } + + public V get(K key) { + if (key == null) { + System.out.println("Error: Key cannot be null"); + return null; + } + V value = cache.get(key); + if (value == null) { + System.out.println("Cache miss: " + key + " not found"); + return null; + } + System.out.println("Cache hit: " + key + " = " + value); + return value; + } + + public void remove(K key) { + cache.remove(key); + System.out.println("Cache removed: " + key); + } + + public void clear() { + cache.clear(); + System.out.println("Cache cleared"); + } + + public int size() { + return cache.size(); + } + + public boolean containsKey(K key) { + return cache.containsKey(key); + } +} \ No newline at end of file diff --git a/src/w8/W88/W88/GenericReflectionExample.java b/src/w8/W88/W88/GenericReflectionExample.java new file mode 100644 index 0000000..d87311f --- /dev/null +++ b/src/w8/W88/W88/GenericReflectionExample.java @@ -0,0 +1,53 @@ +package w8.W88.W88; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +public class GenericReflectionExample { + + public static void getGenericInfo() { + List list = new ArrayList() {}; + + Type type = list.getClass().getGenericSuperclass(); + if (type instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) type; + Type[] actualTypes = pt.getActualTypeArguments(); + for (Type t : actualTypes) { + System.out.println("Generic type: " + t.getTypeName()); + } + } + } + + public static void getFieldGenericInfo() throws NoSuchFieldException { + class MyClass { + public List names; + } + + Field field = MyClass.class.getField("names"); + ParameterizedType type = (ParameterizedType) field.getGenericType(); + Type[] types = type.getActualTypeArguments(); + System.out.println("Field generic type: " + types[0].getTypeName()); + } + + public static void getMethodGenericInfo() throws NoSuchMethodException { + class MyClass { + public void process(List numbers) {} + } + + Method method = MyClass.class.getMethod("process", List.class); + Type[] paramTypes = method.getGenericParameterTypes(); + ParameterizedType pt = (ParameterizedType) paramTypes[0]; + System.out.println("Method parameter generic: " + pt.getActualTypeArguments()[0]); + } + + public static void main(String[] args) throws Exception { + System.out.println("=== Reflection Generic Info ==="); + getGenericInfo(); + getFieldGenericInfo(); + getMethodGenericInfo(); + } +} \ No newline at end of file diff --git a/src/w8/W88/W88/Main.java b/src/w8/W88/W88/Main.java new file mode 100644 index 0000000..a794723 --- /dev/null +++ b/src/w8/W88/W88/Main.java @@ -0,0 +1,33 @@ +package w8.W88.W88; + +import w8.Cache; +import w8.Pair; + +public class Main { + public static void main(String[] args) { + System.out.println("=== Pair Test ==="); + Pair pair = new Pair<>("age", 25); + System.out.println("Original: " + pair); + System.out.println("Swap: " + pair.swap()); + + System.out.println("\n=== Cache Test ==="); + Cache cache = new Cache<>(3000); + cache.put("name", "Ahmed"); + cache.put("city", "Cairo"); + + System.out.println("\n=== Get Values ==="); + System.out.println("Get name: " + cache.get("name")); + System.out.println("Get city: " + cache.get("city")); + System.out.println("Get country: " + cache.get("country")); + + System.out.println("\n=== Remove ==="); + cache.remove("city"); + System.out.println("Get city after remove: " + cache.get("city")); + + System.out.println("\n=== Size ==="); + System.out.println("Cache size: " + cache.size()); + + cache.clear(); + System.out.println("After clear, size: " + cache.size()); + } +} \ No newline at end of file diff --git a/src/w8/W88/W88/Pair.java b/src/w8/W88/W88/Pair.java new file mode 100644 index 0000000..d8e7ec3 --- /dev/null +++ b/src/w8/W88/W88/Pair.java @@ -0,0 +1,36 @@ +package w8.W88.W88; + +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<>(value, key); + } + + @Override + public String toString() { + return "Pair{" + key + ", " + value + "}"; + } +} \ No newline at end of file