#coding = utf-8
#包装
class Student:
def __init__(self,sno=10,age=2):
self.sno = sno
self._age = age #私有变量
@property #包装成不私有的
def age(self):
return self._age
@age.setter
def age(self,age): #包装属性范围
if(0 < age <18):
print('No permission !')
else:
self._age=age
def get_sno(self,sno):
self.sno = sno
def get_age(self,age):
self.age = age
def add_sno(self):
print('sno :',self.sno)
def add_age(self):
print('age :',self.age)
if __name__ == '__main__':
jack = Student()
print(jack.age)
jack.age = 16
print(jack.age)
python_类包装
最新推荐文章于 2023-04-11 09:09:04 发布