class Person(object):
def __init__(self, age=1):
self.age = age
def drive(self):
if self.age >= 18:
print('OK')
else:
raise Exception('No drive')
class Baby(Person):
def __init__(self, age=1):
if age < 18:
super().__init__(age)
else:
raise ValueError
class Adult(Person):
def __init__(self, age=18):
if age >= 18:
super().__init__(age)
else:
raise ValueError
class Car(object):
def __init__(self, model=None):
self.model = model
def run(self):
print('run')
def ride(self, person):
person.drive()
baby = Baby()
adult = Adult()
car = Car()
car.ride(adult)
実行結果
OK
最後の行を
car.ride(baby)
にかえると
実行結果
Exception: No drive