分别创建三个类“轮船”“汽车”和“火车”。
时间: 2024-11-15 21:23:42 浏览: 8
当然,我可以为您描述这三个类的设计:
1. **轮船** 类:
```java
public class 轮船 {
private String name;
private int capacity;
private double length;
// 构造函数
public 轮船(String name, int capacity, double length) {
this.name = name;
this.capacity = capacity;
this.length = length;
}
// 属性访问方法
public String getName() { return name; }
public int getCapacity() { return capacity; }
public double getLength() { return length; }
// 描述轮船功能的方法,比如移动或显示信息
public void sail() {
System.out.println(this.name + "正在海上航行...");
}
}
```
2. **汽车** 类:
```java
public class 汽车 {
private String modelName;
private int passengerCapacity;
private String color;
// 构造函数
public 汽车(String modelName, int passengerCapacity, String color) {
this.modelName = modelName;
this.passengerCapacity = passengerCapacity;
this.color = color;
}
// 获取和设置属性...
// 比如 drive() 方法模拟车辆行驶
public void drive() {
System.out.println(this.modelName + "在公路上行驶...");
}
}
```
3. **火车** 类:
```java
public class 火车 {
private String trainNumber;
private int coachCount;
private String locomotiveType;
// 构造函数
public 火车(String trainNumber, int coachCount, String locomotiveType) {
this.trainNumber = trainNumber;
this.coachCount = coachCount;
this.locomotiveType = locomotiveType;
}
// 属性和方法...
public void startEngine() {
System.out.println(trainNumber + "列车的" + locomotiveType + "开始发动...");
}
}
```
以上每个类都有各自的特点和功能,如名称、容量、颜色等,同时都是`交通工具`这一抽象概念的实例。
阅读全文