class Dog(Animal):
# 类的继承
# 只使用@property装饰器与普通函数做对比
def eating(self):
print(
"I am eating"
)
@property
# 用这个装饰器后这个方法调用就可以不加括号,即将其转化为属性
def running(self):
if
self.age >= 3 and self.age < 130:
print(
"I am running"
)
elif self.age > 0 and self.age <3:
print(
"I can't run"
)
else
:
print(
"please input true age"
)
# 三种装饰器,可以获取、设置、删除这样定义的属性
@property
def country(self):
return
self._country
# 注意这个属性之前从来没有定义过,是在下面的setter中定义的
@country.setter
# 用 函数名.setter 的装饰器
def country(self, value):
# 设置这个属性的值
self._country = value
@country.deleter
def country(self):
print(
"The attr country is deleted"
)
# 用property函数实现和装饰器相同的功能
def get_city(self):
return
self._city
def set_city(self, value):
self._city = value
def del_city(self, value):
del self._city
city = property(get_city, set_city, del_city,
"where it is in"
)
@staticmethod
def print_dog():
print(
"这是Animal的一个子类,主要讲解三个装饰器进行方法向属性的转换"
)
print(
"类继承,创建实例时仍要指定父类的普通属性"
)
print(
"@property装饰器将方法转化为属性方式调用,此时的方法必须只有一个self参数"
)
print(
"使用@property后可以看做一个属性(country),用property函数可以达到相同的效果(city)"
)
print(
"注:city中property第四个参数只是一个说明,用Dog.city.__doc__来调用,即返回 where it is in"
)