(1)定义一个ClassName接口,接口中只有一个抽象方法getClassName()。设计一个类Company,该类实现接口ClassName种的方法getClassName(),功能是获取该类的类名称。编写应用程序使用Company类。 (2)创建一个抽象的车类,包括各种车辆。 成员变量:品牌(brand)、可载人数(capacity)、轮子个数(wheelNum)、能源(energy)等。 抽象方法如:跑(run)、加速(accelerate)、刹车(brake)等。
时间: 2023-05-11 21:02:22 浏览: 200
(1) 代码如下:
public interface ClassName {
public String getClassName();
}
public class Company implements ClassName {
public String getClassName() {
return "Company";
}
}
public class Main {
public static void main(String[] args) {
Company company = new Company();
System.out.println(company.getClassName());
}
}
输出结果为:Company
(2) 代码如下:
public abstract class Vehicle {
protected String brand;
protected int capacity;
protected int wheelNum;
protected String energy;
public abstract void run();
public abstract void accelerate();
public abstract void brake();
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Car extends Vehicle {
public void run() {
System.out.println("Car is running.");
}
public void accelerate() {
System.out.println("Car is accelerating.");
}
public void brake() {
System.out.println("Car is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Bicycle extends Vehicle {
public void run() {
System.out.println("Bicycle is running.");
}
public void accelerate() {
System.out.println("Bicycle is accelerating.");
}
public void brake() {
System.out.println("Bicycle is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Motorcycle extends Vehicle {
public void run() {
System.out.println("Motorcycle is running.");
}
public void accelerate() {
System.out.println("Motorcycle is accelerating.");
}
public void brake() {
System.out.println("Motorcycle is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Bus extends Vehicle {
public void run() {
System.out.println("Bus is running.");
}
public void accelerate() {
System.out.println("Bus is accelerating.");
}
public void brake() {
System.out.println("Bus is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Truck extends Vehicle {
public void run() {
System.out.println("Truck is running.");
}
public void accelerate() {
System.out.println("Truck is accelerating.");
}
public void brake() {
System.out.println("Truck is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Train extends Vehicle {
public void run() {
System.out.println("Train is running.");
}
public void accelerate() {
System.out.println("Train is accelerating.");
}
public void brake() {
System.out.println("Train is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Airplane extends Vehicle {
public void run() {
System.out.println("Airplane is running.");
}
public void accelerate() {
System.out.println("Airplane is accelerating.");
}
public void brake() {
System.out.println("Airplane is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Ship extends Vehicle {
public void run() {
System.out.println("Ship is running.");
}
public void accelerate() {
System.out.println("Ship is accelerating.");
}
public void brake() {
System.out.println("Ship is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Rocket extends Vehicle {
public void run() {
System.out.println("Rocket is running.");
}
public void accelerate() {
System.out.println("Rocket is accelerating.");
}
public void brake() {
System.out.println("Rocket is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Hovercraft extends Vehicle {
public void run() {
System.out.println("Hovercraft is running.");
}
public void accelerate() {
System.out.println("Hovercraft is accelerating.");
}
public void brake() {
System.out.println("Hovercraft is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Submarine extends Vehicle {
public void run() {
System.out.println("Submarine is running.");
}
public void accelerate() {
System.out.println("Submarine is accelerating.");
}
public void brake() {
System.out.println("Submarine is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Helicopter extends Vehicle {
public void run() {
System.out.println("Helicopter is running.");
}
public void accelerate() {
System.out.println("Helicopter is accelerating.");
}
public void brake() {
System.out.println("Helicopter is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Tank extends Vehicle {
public void run() {
System.out.println("Tank is running.");
}
public void accelerate() {
System.out.println("Tank is accelerating.");
}
public void brake() {
System.out.println("Tank is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Bulldozer extends Vehicle {
public void run() {
System.out.println("Bulldozer is running.");
}
public void accelerate() {
System.out.println("Bulldozer is accelerating.");
}
public void brake() {
System.out.println("Bulldozer is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Excavator extends Vehicle {
public void run() {
System.out.println("Excavator is running.");
}
public void accelerate() {
System.out.println("Excavator is accelerating.");
}
public void brake() {
System.out.println("Excavator is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Crane extends Vehicle {
public void run() {
System.out.println("Crane is running.");
}
public void accelerate() {
System.out.println("Crane is accelerating.");
}
public void brake() {
System.out.println("Crane is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Forklift extends Vehicle {
public void run() {
System.out.println("Forklift is running.");
}
public void accelerate() {
System.out.println("Forklift is accelerating.");
}
public void brake() {
System.out.println("Forklift is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class Tractor extends Vehicle {
public void run() {
System.out.println("Tractor is running.");
}
public void accelerate() {
System.out.println("Tractor is accelerating.");
}
public void brake() {
System.out.println("Tractor is braking.");
}
}
// 可以继承Vehicle类,实现具体的车辆类型
public class LawnMower extends Vehicle {
public void run() {
System.out.println("LawnMower is running.");
}
public void accelerate() {
System.out.println("LawnMower is accelerating.");
}
public void brake() {
System.out.println("LawnMower is braking.");
}
}
阅读全文