我试图定义一个具有抽象属性的抽象类,然后可以在具体类中继承。但它给出的是maximum recursion depth exceeded
。Vechile
是一个抽象类,Car
实现了该类。基类有一个名为parking_ticket
的属性,我将其保留在抽象类的__init__
之外,并定义一个名为parking_ticket
的属性作为getter和setter。请帮我指出错误在哪里。
抽象类
"""
module string
"""
from abc import ABC, abstractmethod
from vehicle_type import VehicleType
from parking_ticket import ParkingTicket
class Vechile(ABC):
"""
class docstring
"""
@abstractmethod
def __init__(self, license_plate: str,
kind_of_vehicle: VehicleType) -> None:
self.plate_number = license_plate
self.vehicle_type = kind_of_vehicle
self.parking_ticket = None
@property
@abstractmethod
def parking_ticket(self):
pass
@parking_ticket.setter
@abstractmethod
def parking_ticket(self, parking_ticket: ParkingTicket):
pass
具体类
from vehicle import Vechile
from vehicle_type import VehicleType
from parking_ticket import ParkingTicket
from datetime import datetime, timedelta
class Car(Vechile):
def __init__(self,
license_plate: str) -> None:
super().__init__(license_plate, VehicleType.CAR)
@property
def parking_ticket(self):
return self.parking_ticket
@parking_ticket.setter
def parking_ticket(self,
ticket: ParkingTicket):
self.parking_ticket = ticket
p_ticket = ParkingTicket(ticket_number="123",
plate_number="12345",
allocated_spot_id=10,
issued_at=datetime.utcnow(),
vaccated_at=datetime.utcnow() + timedelta(hours=5),
charges=50
)
c = Car('MH53TS7618')
c.parking_ticket = p_ticket
print(c)
找到了答案。我使用了与方法名和实例变量相同的变量名。因此出现了运行时异常。