1 changed files with 42 additions and 0 deletions
@ -0,0 +1,42 @@ |
|||
// 抽象父类Vehicle |
|||
abstract class Vehicle { |
|||
// 抽象方法run() |
|||
public abstract void run(); |
|||
} |
|||
|
|||
// 子类Car |
|||
class Car extends Vehicle { |
|||
public void run() { |
|||
System.out.println("Car is running"); |
|||
} |
|||
} |
|||
|
|||
// 子类Bike |
|||
class Bike extends Vehicle { |
|||
public void run() { |
|||
System.out.println("Bike is running"); |
|||
} |
|||
} |
|||
|
|||
// 子类Truck |
|||
class Truck extends Vehicle { |
|||
public void run() { |
|||
System.out.println("Truck is running"); |
|||
} |
|||
} |
|||
|
|||
// 测试类 |
|||
class VehicleTest { |
|||
public static void main(String[] args) { |
|||
// 创建Vehicle数组 |
|||
Vehicle[] vehicles = new Vehicle[3]; |
|||
vehicles[0] = new Car(); |
|||
vehicles[1] = new Bike(); |
|||
vehicles[2] = new Truck(); |
|||
|
|||
// 遍历调用run()方法 |
|||
for (Vehicle vehicle : vehicles) { |
|||
vehicle.run(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue