You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
828 B
33 lines
828 B
进阶题——Vehicle
|
|
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 VehicleDemo {
|
|
public static void main(String[] args) {
|
|
Vehicle[] vehicles = new Vehicle[3];
|
|
vehicles[0] = new Car();
|
|
vehicles[1] = new Bike();
|
|
vehicles[2] = new Truck();
|
|
for (Vehicle vehicle : vehicles) {
|
|
vehicle.run();
|
|
}
|
|
}
|
|
}
|