4 changed files with 71 additions and 0 deletions
@ -0,0 +1,38 @@ |
|||
abstract class Vehicle { |
|||
public abstract void run(); |
|||
} |
|||
|
|||
class Car extends Vehicle { |
|||
@Override |
|||
public void run() { |
|||
System.out.println("🚗 小汽车在公路上飞驰"); |
|||
} |
|||
} |
|||
|
|||
class Bike extends Vehicle { |
|||
@Override |
|||
public void run() { |
|||
System.out.println("🚲 自行车在林荫道骑行"); |
|||
} |
|||
} |
|||
|
|||
class Truck extends Vehicle { |
|||
@Override |
|||
public void run() { |
|||
System.out.println("🚚 大卡车在运送货物"); |
|||
} |
|||
} |
|||
|
|||
public class VehicleTest { |
|||
public static void main(String[] args) { |
|||
Vehicle[] vehicles = { |
|||
new Car(), |
|||
new Bike(), |
|||
new Truck() |
|||
}; |
|||
|
|||
for (Vehicle v : vehicles) { |
|||
v.run(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
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 circle = new Circle(); |
|||
drawShape(circle); |
|||
Shape rect = new Rectangle(); |
|||
drawShape(rect); |
|||
drawShape(new Shape()); |
|||
} |
|||
} |
|||
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue