2. 设计Flight类。 要求: (1)具有机型、班次、额定载客数和实际载客数等属性; (2)具有输入/输出属性功能; (3)求载客率。
时间: 2024-03-25 07:38:49 浏览: 80
好的,以下是Flight类的设计:
```python
class Flight:
def __init__(self, aircraft_type, flight_number, passenger_capacity, actual_passenger_number):
self.aircraft_type = aircraft_type
self.flight_number = flight_number
self.passenger_capacity = passenger_capacity
self.actual_passenger_number = actual_passenger_number
def input_attributes(self):
self.aircraft_type = input("请输入机型:")
self.flight_number = input("请输入班次:")
self.passenger_capacity = int(input("请输入额定载客数:"))
self.actual_passenger_number = int(input("请输入实际载客数:"))
def output_attributes(self):
print("机型:", self.aircraft_type)
print("班次:", self.flight_number)
print("额定载客数:", self.passenger_capacity)
print("实际载客数:", self.actual_passenger_number)
def passenger_load_factor(self):
return self.actual_passenger_number / self.passenger_capacity
```
Flight类有四个属性:机型(aircraft_type)、班次(flight_number)、额定载客数(passenger_capacity)和实际载客数(actual_passenger_number)。其中,初始化函数__init__()接收这四个属性作为参数,并将它们赋值给实例变量。类还有三个方法:input_attributes()、output_attributes()和passenger_load_factor()。
input_attributes()方法用于输入对象的属性值,通过input()函数获取用户输入的值,然后将它们分别赋值给实例变量。
output_attributes()方法用于输出对象的属性值,通过print()函数将对象的属性值一一输出。
passenger_load_factor()方法用于计算载客率,返回实际载客数与额定载客数的比值。
阅读全文