super 子類和父類初始化例子
阿新 • • 發佈:2018-04-28
n) elf flag python highlight 初始 class PE bject
# #重寫父類初始化 class Person(object): def __init__(self, flag=False, name="jim"): self.name = name self.flag = flag print("Person",self.name) print("Person", self.flag) class c(Person): def __init__(self,flag,name,age ): super(c, self).__init__(flag,name) self.age = age print("c‘s flag:", self.flag) print("c‘s name:", self.name) print("c‘s age:",self.age) coder = c(flag=True,name="aaa",age=21) # 結果 # Person aaa # Person True # c‘s flag: True # c‘s name: aaa # c‘s age: 21 #繼承父類的初始化 # class Person(object): # def __init__(self, flag=False, name="jim"): # self.name = name # self.flag = flag # print("Person",self.name) # print("Person", self.flag) # # # # class c(Person): # def __init__(self,age ): # super(c, self).__init__() # self.age = age # print("c‘s flag:", self.flag) # print("c‘s name:", self.name) # print("c‘s age:",self.age) # # # coder = c(age=21) #結果 # Person jim # Person False # c‘s flag: False # c‘s name: jim # c‘s age: 21
super 子類和父類初始化例子