码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象之property

时间:2018-02-22 16:05:36      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:int   init   对象   color   ini   bsp   log   功能   post   

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

标签:int   init   对象   color   ini   bsp   log   功能   post   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!