You need to enable JavaScript to run this app. Python面向对象开发下如何按影片维度标记座位为已占用
优惠活动
大模型
产品
解决方案
定价
更多

Python面向对象开发下如何按影片维度标记座位为已占用

现有代码核心问题定位
  • 继承实现笔误:cinema类初始化时调用了不存在的pelicula.__init__方法,会直接触发运行报错
  • 座位类实例化参数顺序错位:seat类构造函数定义的参数顺序为(位置, 占用状态, 关联影片名, 关联放映时间),但实际创建实例时传入的参数顺序是(位置, 影片名, 放映时间, 占用状态),所有属性值存储错位,无法正确读取座位状态
  • 全局座位表设计缺陷:单个数组存储所有座位的状态,同一个物理座位会被所有场次共享状态,无法实现“同座位在不同影片场次下分别标记占用”的需求
  • 数组元素类型不统一:初始数组元素为字符串类型(如'8A'),临时插入seat类实例,打印时会同时出现字符串、类对象内存地址,显示逻辑混乱
修正后的面向对象实现

按照单一职责原则拆分类功能,每个类仅负责对应领域的逻辑,从结构上实现按场次隔离座位状态:

from random import randint

# 影片类:存储影片固有属性,和放映场景解耦
class Movie:
    def __init__(self, title, duration, minimum_age, director):
        self.title = title
        self.duration = duration
        self.minimum_age = minimum_age
        self.director = director

# 座位类:封装座位的位置、状态、显示规则
class Seat:
    def __init__(self, position):
        self.position = position
        self.is_occupied = False
        self.linked_screening = None

    def mark_occupied(self, current_screening):
        # 座位被指定场次占用时直接修改自身状态
        self.is_occupied = True
        self.linked_screening = current_screening

    def render_display(self):
        # 根据自身状态返回显示内容,不需要外部手动替换数组元素
        return "XX" if self.is_occupied else self.position

# 放映场次类:关联影片信息、单场次票价/时间,持有独立座位表
class Screening(Movie):
    def __init__(self, base_movie, price, hour):
        # 调用父类初始化传入影片固有属性
        super().__init__(
            title=base_movie.title,
            duration=base_movie.duration,
            minimum_age=base_movie.minimum_age,
            director=base_movie.director
        )
        self.price = price
        self.hour = hour
        # 每个场次独立初始化座位表,从根源上避免跨场次状态串扰
        self.seat_grid = self._build_seat_grid()

    def _build_seat_grid(self):
        # 生成8排9列的标准座位表,每个位置是独立的Seat实例
        seat_grid = []
        row_total = 8
        col_identifiers = ['A','B','C','D','E','F','G','H','I']
        for row_idx in range(row_total):
            current_row = []
            for col_idx in range(len(col_identifiers)):
                seat_position = f"{row_total - row_idx}{col_identifiers[col_idx]}"
                current_row.append(Seat(seat_position))
            seat_grid.append(current_row)
        return seat_grid

    def show_seat_layout(self):
        # 打印当前场次的完整座位图
        print(f"\n当前场次:{self.title} | 放映时间:{self.hour} | 票价:{self.price}")
        for row in self.seat_grid:
            for seat in row:
                print(f"{seat.render_display():<4}", end="")
            print()

    def randomly_occupy_seat(self):
        # 随机选取位置标记为已售
        selected_row = randint(0, 7)
        selected_col = randint(0, 8)
        target_seat = self.seat_grid[selected_row][selected_col]
        target_seat.mark_occupied(self)
        print(f"已选中座位:{target_seat.position}")
功能验证代码
# 创建两部影片的基础信息
interstellar = Movie(title="星际穿越", duration=169, minimum_age=10, director="克里斯托弗·诺兰")
nezha = Movie(title="哪吒之魔童降世", duration=110, minimum_age=0, director="饺子")

# 创建两个独立的放映场次,自动生成各自的座位表
screening_1430 = Screening(interstellar, price=45, hour="14:30")
screening_1645 = Screening(nezha, price=38, hour="16:45")

# 分别为两个场次随机标记2个已占座位
screening_1430.randomly_occupy_seat()
screening_1430.randomly_occupy_seat()
screening_1645.randomly_occupy_seat()
screening_1645.randomly_occupy_seat()

# 分别打印两个场次的座位布局,状态完全独立
screening_1430.show_seat_layout()
screening_1645.show_seat_layout()

实现说明

  • 不需要手动修改全局数组的元素值:座位的显示状态由Seat类自身根据属性判断返回,不会出现类对象内存地址打印异常的问题
  • 跨场次状态完全隔离:哪怕两个场次选中了同一个物理位置(比如8A)标记为已占,各自的座位表只会在自身的数据结构里修改状态,不会影响其他场次的显示
  • 符合面向对象的设计要求:每个类的职责清晰,没有跨类的耦合逻辑

内容的提问来源于stack exchange,提问作者Brandol Cortez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.24 13:45:10