# property
# 內(nèi)置裝飾器函數(shù) 只在面向?qū)ο笾惺褂?
from math import pi
class Circle:
def __init__(self,r):
self.r = r
@property
def perimeter(self):
return 2*pi*self.r
@property
def area(self):
return self.r**2*pi
c1 = Circle(5)
print(c1.area) # 圓的面積
print(c1.perimeter) # 圓的周長
class Person:
def __init__(self,name,high,weight):
self.name = name
self.high = high
self.weight = weight
@property
def bmi(self):
return self.weight / self.high**2
jin = Person('金老板',1.6,90)
jin.bmi = 18
classmethod
staticmethod
class Person:
def __init__(self,name):
self.__name = name
@property
def name(self):
return self.__name + 'sb'
@name.setter
def name(self,new_name):
self.__name = new_name
tiger = Person('泰哥')
print(tiger.name)
tiger.name = '全班'
print(tiger.name)
class Goods:
discount = 0.8
def __init__(self,name,price):
self.name = name
self.__price = price
@property
def price(self):
return self.__price * Goods.discount
apple = Goods('蘋果',5)
print(apple.price)
# 屬性 查看 修改 刪除
class Person:
def __init__(self,name):
self.__name = name
self.price = 20
@property
def name(self):
return self.__name
@name.deleter
def name(self):
del self.__name
@name.setter
def name(self,new_name):
self.__name = new_name
brother2 = Person('二哥')
del Person.price
brother2.name = 'newName'
brother2
del brother2.name
print(brother2.name)
# 內(nèi)置裝飾器函數(shù) 只在面向?qū)ο笾惺褂?
from math import pi
class Circle:
def __init__(self,r):
self.r = r
@property
def perimeter(self):
return 2*pi*self.r
@property
def area(self):
return self.r**2*pi
c1 = Circle(5)
print(c1.area) # 圓的面積
print(c1.perimeter) # 圓的周長
class Person:
def __init__(self,name,high,weight):
self.name = name
self.high = high
self.weight = weight
@property
def bmi(self):
return self.weight / self.high**2
jin = Person('金老板',1.6,90)
jin.bmi = 18
classmethod
staticmethod
class Person:
def __init__(self,name):
self.__name = name
@property
def name(self):
return self.__name + 'sb'
@name.setter
def name(self,new_name):
self.__name = new_name
tiger = Person('泰哥')
print(tiger.name)
tiger.name = '全班'
print(tiger.name)
class Goods:
discount = 0.8
def __init__(self,name,price):
self.name = name
self.__price = price
@property
def price(self):
return self.__price * Goods.discount
apple = Goods('蘋果',5)
print(apple.price)
# 屬性 查看 修改 刪除
class Person:
def __init__(self,name):
self.__name = name
self.price = 20
@property
def name(self):
return self.__name
@name.deleter
def name(self):
del self.__name
@name.setter
def name(self,new_name):
self.__name = new_name
brother2 = Person('二哥')
del Person.price
brother2.name = 'newName'
brother2
del brother2.name
print(brother2.name)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
