# BMI指数(bmi是计算而来,但很明显它听起来像是一个属性而非方法,如果我们将其做成一个属性,更便于理解)
'''
成人的BMI数字值:
过轻:低于18.5
正常:18.5-23.9
过重:24-27
肥胖:28-32
非常肥胖:高于32
体质指数(BMI)= 体重(kg)/身高^2(m)
EX:70kg/(1.75*1.75)=22.86
'''
class People:
def __init__(self,name,weight,height):
self.name=name
self.weight=weight
self.height=height
@property
def bmi(self):
return self.weight/(self.height ** 2)
p1 = People('kingforn', 85, 1.70)
print(p1.bmi) # 29.411764705882355
class People:
def __init__(self,name):
self.__name=name
@property
def name(self):
return self.__name
@name.setter
def name(self,val):
if not isinstance(val,str):
print('名字必需是要字符串')
return
self.__name = val
print(self.__name)
@name.deleter
def name(self):
del self.__name
print('用户已删除')
print(self.__dict__)
p = People('kingforn')
print(p.name) # kingforn
p.name = 'kingforn1'
del p.name
'''
kingforn1
用户已删除
{}
'''