15 changed files with 181 additions and 141 deletions
@ -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(); |
|||
} |
|||
} |
|||
} |
|||
@ -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 接口)
|
|||
} |
|||
} |
|||
@ -1,40 +0,0 @@ |
|||
package w8; |
|||
public class Main { |
|||
public static void main(String[] args) throws InterruptedException { |
|||
// Pair test
|
|||
Pair<String, Integer> pair = new Pair<>("age", 25); |
|||
System.out.println("Original: " + pair); |
|||
System.out.println("Swap: " + pair.swap()); |
|||
|
|||
// Cache test
|
|||
Cache<String, String> cache = new Cache<>(3000); |
|||
cache.put("name", "Ahmed"); |
|||
System.out.println("Cache get: " + cache.get("name")); |
|||
} |
|||
} |
|||
|
|||
class Pair<K, V> { |
|||
private K key; |
|||
private V value; |
|||
public Pair(K key, V value) { this.key = key; this.value = value; } |
|||
public Pair<V, K> swap() { return new Pair<>(value, key); } |
|||
public String toString() { return "Pair{" + key + "," + value + "}"; } |
|||
} |
|||
|
|||
class Cache<K, V> { |
|||
private java.util.HashMap<K, CacheEntry<V>> 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<V> entry = cache.get(key); |
|||
if (entry == null || (entry.expireTime > 0 && System.currentTimeMillis() > entry.expireTime)) return null; |
|||
return entry.value; |
|||
} |
|||
private static class CacheEntry<V> { |
|||
V value; long expireTime; |
|||
CacheEntry(V v, long e) { value = v; expireTime = e; } |
|||
} |
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
package w8.W88.W88; |
|||
|
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
|
|||
public class Cache<K, V> { |
|||
private ConcurrentHashMap<K, V> 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); |
|||
} |
|||
} |
|||
@ -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<String> list = new ArrayList<String>() {}; |
|||
|
|||
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<String> 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<Integer> 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(); |
|||
} |
|||
} |
|||
@ -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<String, Integer> pair = new Pair<>("age", 25); |
|||
System.out.println("Original: " + pair); |
|||
System.out.println("Swap: " + pair.swap()); |
|||
|
|||
System.out.println("\n=== Cache Test ==="); |
|||
Cache<String, String> 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()); |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
package w8.W88.W88; |
|||
|
|||
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 V getValue() { |
|||
return value; |
|||
} |
|||
|
|||
public void setKey(K key) { |
|||
this.key = key; |
|||
} |
|||
|
|||
public void setValue(V value) { |
|||
this.value = value; |
|||
} |
|||
|
|||
public Pair<V, K> swap() { |
|||
return new Pair<>(value, key); |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return "Pair{" + key + ", " + value + "}"; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue