class Person:
def __init__(self,name):
self.name=name
class MPerson(Person):
def __init__(self,name, age):
super().__init__(name)
self.age=age
class Person:
def init(self,name):
self.name=name
def prt(self):
print(self.name)
def set_age(self, age):
self.age=age
f=Person()
f.init('hhh')#自定義初始化
print(f.name)
f.prt()#自定義初始化的值可以內部被調用
Person.set_age(f, 27)#可以先調用類中方法,再把實例化對象f轉為self傳入給該方法
print(f.age)
b=MPerson('xiaohong',29)#輸入Person類修改了,但是MPerson繼承時以其之前的Person為准
print(b.name)
輸出:
hhh
hhh
27
xiaohong