# 定义一个父类
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def show(self):
print('name: ', self.name, ' age: ', self.age)
# 子类 American
class American(Person):
pass
# 子类 Chinese
class Chinese(Person):
# 重写父类 __init__ 方法
def __init__(self, name, age, sex):
# Person.__init__(self)
super().__init__(name, age)
self.sex = sex
def show(self):
print('name: ', self.name, ' age: ', self.age, ' sex: ', self.sex)
# 测试
print(American.__bases__) # <class '__main__.Person'>
# 实例化
a = American('Tom', 68)
a.show() # name: Tom age: 68
c = Chinese('老王', 18, '男')
c.show() # name: 老王 age: 18 sex: 男