diff --git a/w5/AI协助记录 b/w5/AI协助记录 new file mode 100644 index 0000000..b3318df --- /dev/null +++ b/w5/AI协助记录 @@ -0,0 +1,4 @@ +· 帮我理清了多态中“编译看左边,运行看右边”的底层机制 +· 解释了为什么父类引用调用子类重写方法时会执行子类逻辑 +· 讲解了Java中public类与文件名的强制对应关系 +· 说明了单文件编写时非public类的正确写法 \ No newline at end of file diff --git a/w5/Pair.java b/w5/Pair.java new file mode 100644 index 0000000..3b43730 --- /dev/null +++ b/w5/Pair.java @@ -0,0 +1,33 @@ +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 void setKey(K key) { this.key = key; } + public V getValue() { return value; } + public void setValue(V value) { this.value = value; } + + // 交换键值的静态方法 + public static Pair swap(Pair pair) { + return new Pair<>(pair.getValue(), pair.getKey()); + } + + @Override + public String toString() { + return "Pair{" + "key=" + key + ", value=" + value + '}'; + } + + public static void main(String[] args) { + // 测试 + Pair pair = new Pair<>("age", 18); + System.out.println("交换前:" + pair); + + Pair swappedPair = Pair.swap(pair); + System.out.println("交换后:" + swappedPair); + } +} \ No newline at end of file diff --git a/w5/ShapeTest.java b/w5/ShapeTest.java new file mode 100644 index 0000000..091e5c5 --- /dev/null +++ b/w5/ShapeTest.java @@ -0,0 +1,35 @@ +class Shape { + public void draw() { + System.out.println("绘制一个形状"); + } +} + +class Circle extends Shape { + @Override + public void draw() { + System.out.println("绘制一个圆形"); + } +} + +class Rectangle extends Shape { + @Override + public void draw() { + System.out.println("绘制一个矩形"); + } +} + +public class ShapeTest { + public static void drawShape(Shape s) { + s.draw(); + } + + public static void main(String[] args) { + Shape shape1 = new Circle(); + Shape shape2 = new Rectangle(); + Shape shape3 = new Shape(); + + drawShape(shape1); + drawShape(shape2); + drawShape(shape3); + } +} \ No newline at end of file diff --git a/w5/USBTest.java b/w5/USBTest.java new file mode 100644 index 0000000..5a655e9 --- /dev/null +++ b/w5/USBTest.java @@ -0,0 +1,55 @@ +// 接口 +interface USB { + void open(); + void close(); +} + +// Mouse类实现USB接口 +class Mouse implements USB { + @Override + public void open() { + System.out.println("鼠标已连接,可以移动光标"); + } + + @Override + public void close() { + System.out.println("鼠标已断开"); + } +} + +// Keyboard类实现USB接口 +class Keyboard implements USB { + @Override + public void open() { + System.out.println("键盘已连接,可以输入文字"); + } + + @Override + public void close() { + System.out.println("键盘已断开"); + } +} + +// Computer类 +class Computer { + public void useUSB(USB device) { + System.out.println(">>> 正在接入设备..."); + device.open(); + System.out.println("<<< 正在拔出设备..."); + device.close(); + System.out.println(); + } +} + +// 主类(文件名必须是 USBTest.java) +public class USBTest { + public static void main(String[] args) { + Computer computer = new Computer(); + + USB mouse = new Mouse(); + USB keyboard = new Keyboard(); + + computer.useUSB(mouse); + computer.useUSB(keyboard); + } +} diff --git a/w5/截屏2026-04-06 13.01.27.png b/w5/截屏2026-04-06 13.01.27.png new file mode 100644 index 0000000..58c0b00 Binary files /dev/null and b/w5/截屏2026-04-06 13.01.27.png differ