#测试@property的最简化用法
class Employee:
@property
def salary(self):
print('salary')
return 10000
emp1=Employee()
#emp1.salary()
print(emp1.salary)
salary
10000
用property装饰器代写注释内内容
#@property装饰器的用法
class Employee:
def __init__(self,name,salary):
self.__name=name
self.__salary=salary
@property
def salary(self):
return self.__salary
@salary.setter
def salary(self,salary):
if 1000 < salary < 50000:
self.__salary = salary
else:
print('录入错误,薪水在1K在50K之间')
'''
def get_salary(self):
return self.__salary
def set_salary(self,salary):
if 1000<salary<50000:
self.__salary=salary
else:
print('录入错误,薪水在1K在50K之间')
'''
emp1=Employee('dxz',30000)
#print(emp1.salary)
#emp1.salary=-20000
#print(emp1.salary)
emp1.salary=2000
print(emp1.salary)
运行结果
2000