访问列表中的对象类Python

2024-09-27 20:18:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于在python中从列表中访问对象类的问题,我已经将我的案例与堆栈溢出问题进行了比较,但是没有成功。我提出的问题如下

    1. 课程预订的属性有开始日期、结束日期
from datetime import *

class Booking:

    def __init__(self):

        self.year = int(input("Enter the Year: "))
        self.month = int(input("Enter the Month: "))
        self.day = int(input("Enter the Day: "))
        self.start_date = datetime.now()
        self.end_date = datetime(self.year, self.month, self.day)
    1. 教室是抽象的班级
    • 我创建一个booking(list)来存储Booking类的对象,该类在Room类初始化时初始化
from abc import *
from booking import Booking

class Room(ABC):
    bookings = []

    def __init__(self, price, capacity):
        self.booking = Booking()
        self.price = price
        self.capacity = capacity
        self.bookings.append(self.booking)

    @abstractmethod
    def is_booked(self, start_date, end_date):
        pass
    1. SingleBed继承{},价格=150,资本=2
from room import Room

class Singlebed(Room):

    def __init__(self):
        super(Singlebed, self).__init__(150, 2)

    def is_booked(self, start_date, end_date):
        if start_date >= end_date:
            print("EndDate must be greater than StartDate")
        is_Booked = False
        for check_booking in self.bookings:
            is_Booked = check_booking... (I wants to access objects.end_date in class Booking is saved in bookings)

single1 = Singlebed()
single2 = Singlebed()
single3 = Singlebed()
single4 = Singlebed()
single5 = Singlebed()

我的问题:如何访问初始化为single1、single2、


Tags: fromimportselfdateinitisdefstart
1条回答
网友
1楼 · 发布于 2024-09-27 20:18:02

您正在迭代self.bookings,因此“check\u booking”是您的预订实例,包含属性start\u date、end\u date、year、month、day

   def is_booked(self, start_date, end_date):
        if start_date >= end_date:
            print("EndDate must be greater than StartDate")
        is_Booked = False
        for check_booking in self.bookings:
            is_Booked = check_booking.start_date

相关问题 更多 >

    热门问题