首页 > 其他 > 详细

面向对象之property

时间:2018-02-22 16:01:49      阅读:194      评论:0      收藏:0      [点我收藏+]

property功能

  1. 以调用数据属性的方式(不用加括号)调用方法
  2. 方法定义成数据属性(方法本应该是动词)

 

# 定义property之前
class People:
    def __init__(self,name,age,height,weight):
        self.name=name
        self.age=age
        self.height=height
        self.weight=weight
    def bmi(self):
        return self.weight /(self.height*self.height)

whq=People(whq,20,1.65,65)
whq.height=1.82
print(whq.bmi())

# 定义property之后
class People:
    def __init__(self,name,age,height,weight):
        self.name=name
        self.age=age
        self.height=height
        self.weight=weight
    @property
    def bmi(self):
        return self.weight /(self.height*self.height)

whq=People(whq,20,1.65,65)
whq.height=1.82
print(whq.bmi)#此处不用加括号调用

 

面向对象之property

原文:https://www.cnblogs.com/wanghuaqiang/p/8458298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!