1 ''' 2 继承:子类复用父类属性、方法 3 ''' 4 5 6 class Parent(): 7 8 def __init__(self): 9 self.name = 'Allen' 10 11 def say(self): 12 print(f'say name:{self.name}') 13 14 15 # 子类什么属性、方法都没有定义,但它继承了Parent就有了name属性、say方法 16 class Child(Parent): 17 pass 18 19 20 child = Child() 21 child.say() # 会打印 say name:Allen 22 print(child.name) # Allen
标签:name,Parent,子类,self,复用,say,父类,属性 From: https://www.cnblogs.com/allenxx/p/17566972.html