设计一个交通工具类,包含属性:品牌、颜色、最高时速,包含方法:驾驶、显示基本信息。
时间: 2024-05-15 11:17:14 浏览: 27
好的,这是一个关于设计交通工具类的问题。根据您的要求,我可以设计一个包含品牌、颜色、最高时速属性以及驾驶、显示基本信息方法的交通工具类,以便使用者方便地驾驶、显示基本信息。您可以参考以下代码:
```
class Vehicle {
String brand;
String color;
int maxSpeed;
public Vehicle(String brand, String color, int maxSpeed) {
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}
public void drive() {
System.out.println("I am driving at " + maxSpeed + " km/h!");
}
public void showInfo() {
System.out.println("Brand: " + brand);
System.out.println("Color: " + color);
System.out.println("Max Speed: " + maxSpeed + " km/h");
}
}
```
这样,您就可以使用这个交通工具类来创建交通工具,并且调用驾驶和显示基本信息的方法了。希望这个答案能够帮助到您!
相关问题
(1)设计一个交通工具类,包含属性:品牌、颜色、最高时速,包含方法:驾驶、显示基本信息。 (2)设计3个子类:汽车、飞机、轮船。 (3)在测试类中,分别调用3个子类的驾驶的方法、显示基本信息的方法。
1. 交通工具类:
public class Vehicle {
private String brand;
private String color;
private int maxSpeed;
public Vehicle(String brand, String color, int maxSpeed) {
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}
public void drive() {
System.out.println("正在驾驶" + brand);
}
public void displayInfo() {
System.out.println("品牌:" + brand);
System.out.println("颜色:" + color);
System.out.println("最高时速:" + maxSpeed + "km/h");
}
}
2. 汽车类:
public class Car extends Vehicle {
public Car(String brand, String color, int maxSpeed) {
super(brand, color, maxSpeed);
}
}
3. 飞机类:
public class Airplane extends Vehicle {
public Airplane(String brand, String color, int maxSpeed) {
super(brand, color, maxSpeed);
}
}
4. 轮船类:
public class Ship extends Vehicle {
public Ship(String brand, String color, int maxSpeed) {
super(brand, color, maxSpeed);
}
}
5. 测试类:
public class Test {
public static void main(String[] args) {
Car car = new Car("奔驰", "黑色", 200);
Airplane airplane = new Airplane("波音747", "白色", 800);
Ship ship = new Ship("皇家加勒比", "蓝色", 50);
car.drive();
car.displayInfo();
airplane.drive();
airplane.displayInfo();
ship.drive();
ship.displayInfo();
}
}
输出结果:
正在驾驶奔驰
品牌:奔驰
颜色:黑色
最高时速:200km/h
正在驾驶波音747
品牌:波音747
颜色:白色
最高时速:800km/h
正在驾驶皇家加勒比
品牌:皇家加勒比
颜色:蓝色
最高时速:50km/h
java(1)设计一个交通工具类,包含属性:品牌、颜色、最高时速,包含方法:驾驶、显示基本信息。 (2)设计3个子类:汽车、飞机、轮船。 (3)在测试类中,分别调用3个子类的驾驶的方法、显示基本信息的方法。
答案:
好的,这是一个关于Java的问题。下面是我的回答:
为了设计交通工具类,我们可以使用Java中的类和对象。交通工具类有三个属性,分别是品牌、颜色和最高速度。可以使用Java中的属性来存储这些信息。驾驶和显示基本信息是两个方法,我们可以使用Java中的方法来实现它们。至于子类的设计,我们可以模拟汽车、飞机和船的特点来实现。
以下是示例代码:
```java
class Transportation {
String brand;
String color;
double maxSpeed;
public void drive() {
System.out.println("驾驶交通工具,出发!");
}
public void showInfo() {
System.out.println("品牌:" + brand);
System.out.println("颜色:" + color);
System.out.println("最高时速:" + maxSpeed + "km/h");
}
}
class Car extends Transportation {
public Car() {
brand = "奔驰";
color = "银色";
maxSpeed = 240;
}
}
class Airplane extends Transportation {
public Airplane() {
brand = "波音747";
color = "白色";
maxSpeed = 900;
}
}
class Ship extends Transportation {
public Ship() {
brand = "海洋之星";
color = "蓝色";
maxSpeed = 80;
}
}
public class TestTransportation {
public static void main(String[] args) {
Car car = new Car();
car.drive();
car.showInfo();
Airplane airplane = new Airplane();
airplane.drive();
airplane.showInfo();
Ship ship = new Ship();
ship.drive();
ship.showInfo();
}
}
```
如果您想了解更多关于Java的内容,可以在CSDN上查找相关的教程和资料。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)