python-- Object Oriented - Encapsulated

# Package:

读写限制:
class Garment:
def __init__(self, name, price):
self.name = name
self.price = price

@property
def price(self):
return self.__price

@price.setter
def price(self, value):
if 1 <= value <= 1500:
self.__price = value
else:
raise ValueError('overstep price')

# price = property(None, _set_price)


Read only restriction
class Garment:
DEF the __init __ (Self, name,. Price):
the self.name name =
Self .__. Price. Price =

@property
def price(self):
return self.__price

 

 

Write read-only restrictions


class Garment:
def __init__(self, name, price):
self.name = name
self.__price = price

#@property
#def price(self):
# return self.__price

# @price.setter

def _set_price(self, value):
if 1 <= value <= 1500:
self.__price = value
else:
raise ValueError('overstep price')

price = property(None, _set_price)

 

Guess you like

Origin www.cnblogs.com/chenlulu1122/p/11922120.html