设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。在主程序中定义一个car类对象,对其车轮个数、车重、载人数进行设置并显示。
时间: 2023-06-18 13:07:29 浏览: 146
以下是Python语言的实现:
```python
class Vehicle:
def __init__(self, wheels, weight):
self.wheels = wheels
self.weight = weight
def display_vehicle_data(self):
print("Vehicle data:")
print("Number of wheels:", self.wheels)
print("Weight:", self.weight, "kg")
class Car(Vehicle):
def __init__(self, wheels, weight, passenger_load):
super().__init__(wheels, weight)
self.passenger_load = passenger_load
def display_car_data(self):
print("Car data:")
print("Number of wheels:", self.wheels)
print("Weight:", self.weight, "kg")
print("Passenger load:", self.passenger_load)
# 示例
car = Car(4, 1000, 5)
car.display_car_data()
```
运行结果:
```
Car data:
Number of wheels: 4
Weight: 1000 kg
Passenger load: 5
```
阅读全文