class CAnimal:
def __init__(self,param):
print("CAnimal init method called.")
self.name = param
#property 定义一个属性name
@property
def name(self):
return self.__name
#为属性 name 定义修改器
@name.setter
def name(self,name):
self.__name = name
def show(self):
print(self.name,"is an animal")
def __del__(self):
print("CAnimal del method called")
class CDog(CAnimal):
def __init__(self,name,year):
CAnimal.__init__(self,name)
self.year = year
print("CDog init method called")
@property
def year(self,data):
self.__year= data
def show(self):
print(self.name,self.year,"is an dog")
def __del__(self):
print("CDog del method called.")
dog =CDog("habagou",5)
dog.show()
结果:
CAnimal init method called.
CDog init method called
('habagou', 5, 'is an dog')
CDog del method called.