长途汽车、飞机、轮船、火车、出租车、轮车都是交通工具,都卖票。请分别用抽象类和接口组织它们。
时间: 2023-05-20 10:07:14 浏览: 85
实验 8抽象类和接口的应用.zip
好的,以下是我组织这些交通工具的方式:
1. 抽象类 Transportation(交通工具)
```python
from abc import ABC, abstractmethod
class Transportation(ABC):
def __init__(self):
self.ticket_price = None
@abstractmethod
def sell_ticket(self):
pass
class Bus(Transportation):
def __init__(self):
self.ticket_price = 50
def sell_ticket(self):
print("售卖长途汽车票,价格为{}元".format(self.ticket_price))
class Plane(Transportation):
def __init__(self):
self.ticket_price = 1000
def sell_ticket(self):
print("售卖飞机票,价格为{}元".format(self.ticket_price))
class Ship(Transportation):
def __init__(self):
self.ticket_price = 500
def sell_ticket(self):
print("售卖轮船票,价格为{}元".format(self.ticket_price))
class Train(Transportation):
def __init__(self):
self.ticket_price = 200
def sell_ticket(self):
print("售卖火车票,价格为{}元".format(self.ticket_price))
class Taxi(Transportation):
def __init__(self):
self.ticket_price = 10
def sell_ticket(self):
print("售卖出租车票,价格为{}元".format(self.ticket_price))
class Bicycle(Transportation):
def __init__(self):
self.ticket_price = 2
def sell_ticket(self):
print("售卖轮车票,价格为{}元".format(self.ticket_price))
```
2. 接口 TicketSeller(售票员)
```python
from abc import ABC, abstractmethod
class TicketSeller(ABC):
@abstractmethod
def sell_ticket(self):
pass
class Bus(TicketSeller):
def sell_ticket(self):
print("售卖长途汽车票")
class Plane(TicketSeller):
def sell_ticket(self):
print("售卖飞机票")
class Ship(TicketSeller):
def sell_ticket(self):
print("售卖轮船票")
class Train(TicketSeller):
def sell_ticket(self):
print("售卖火车票")
class Taxi(TicketSeller):
def sell_ticket(self):
print("售卖出租车票")
class Bicycle(TicketSeller):
def sell_ticket(self):
print("售卖轮车票")
```
以上是我组织这些交通工具的方式,你可以根据自己的理解和需要进行修改。
阅读全文