classbook:__author=''__name=''__page=0price=0__press=''a=book()# a.__author # 调用属性 __author# 会跑出如下异常信息# AttributeError: book instance has no attribute '__author'print(a.price)# 0a.price=20print(a.price)# 20# print(a.__name)# 报错内容如下:# AttributeError: book instance has no attribute '__name'# print(a.__page)# 报错内容如下:# AttributeError: book instance has no attribute '__page'
classbook:__author=''__name=''__page=0price=0__press=''def__check(self,item):ifitem=='':return0else:return1defshow(self):ifself.__check(self.__author):print(self.__author)else:print('No value')ifself.__check(self.__name):print(self.__name)else:print('No value')defsetname(self,name):self.__name=namea=book()a.show()a.setname('John')a.show()# a.__check() # 调用类的私有方法,结果出错# 报错内容如下;# AttributeError: book instance has no attribute '__check'
classbook:__author=''__name=''__page=0price=0__press=''def__check(self,item):ifitem=='':return0else:return1defshow(self):ifself.__check(self.__author):print(self.__author)else:print('No value')ifself.__check(self.__name):print(self.__name)else:print('No value')defsetname(self,name):self.__name=namedef__init__(self,author,name):self.__author=authorself.__name=namea=book('John','I like this book')a.show()# John# I like this booka.setname('About John')a.show()# John# About John