5 changed files with 127 additions and 0 deletions
@ -0,0 +1,4 @@ |
|||||
|
· 帮我理清了多态中“编译看左边,运行看右边”的底层机制 |
||||
|
· 解释了为什么父类引用调用子类重写方法时会执行子类逻辑 |
||||
|
· 讲解了Java中public类与文件名的强制对应关系 |
||||
|
· 说明了单文件编写时非public类的正确写法 |
||||
@ -0,0 +1,33 @@ |
|||||
|
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 void setKey(K key) { this.key = key; } |
||||
|
public V getValue() { return value; } |
||||
|
public void setValue(V value) { this.value = value; } |
||||
|
|
||||
|
// 交换键值的静态方法
|
||||
|
public static <K, V> Pair<V, K> swap(Pair<K, V> 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<String, Integer> pair = new Pair<>("age", 18); |
||||
|
System.out.println("交换前:" + pair); |
||||
|
|
||||
|
Pair<Integer, String> swappedPair = Pair.swap(pair); |
||||
|
System.out.println("交换后:" + swappedPair); |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 479 KiB |
Loading…
Reference in new issue