class Car: def __init__(self, owner, color, model): self.owner = owner self.color = color self.model = model def open(self): print(f"{self.owner}さんの{self.color}の{self.model}のドアが開いた!") def run(self): print(f"{self.owner}さんの{self.color}の{self.model}が走った!") red_car = Car("田中", "赤", "乗用車") red_car.open() red_car.run() print("-"*50) white_car = Car("鈴木", "白", "トラック") white_car.open() white_car.run() class Machine: def __init__(self, owner, color, model): self.owner = owner self.color = color self.model = model def open(self): print(f"{self.owner}さんの{self.color}の{self.model}のドアが開いた!") def move(self): # 「走る」「飛ぶ」など様々な乗り物が動く機能を抽象化する print(f"{self.owner}さんの{self.color}の{self.model}が動いた!") class Car(Machine): def move(self): # Machineクラスのmoveを上書きして「走る」にする print(f"{self.owner}さんの{self.color}の{self.model}が走った!") class Airplane(Machine): def move(self): # Machineクラスのmoveを上書きして「飛ぶ」にする print(f"{self.owner}さんの{self.color}の{self.model}が飛んだ!") red_car = Car("田中", "赤", "乗用車") red_car.open() red_car.move() print("-"*50) blue_airplane = Airplane("佐藤", "青", "ヘリ") blue_airplane.open() blue_airplane.move()