您的位置: 网站首页> python面向对象> 当前文章
私有属性私有方法在子类中也不可以访问
老董-我爱我家房产SEO2020-04-06185围观,111赞
私有属性私有方法在子类中也不可以访问,这个知识点需要单独感受下。
# -*- coding:utf-8 -*- class Person: __Country = '华夏' def __init__(self,name,hobby): self.name = name self.__hobby = hobby def __skill(self): print('拳打脚踢') class TianLong(Person): def __init__(self,name,hobby): super(TianLong, self).__init__(name,hobby) print('TianLong子类对象创建完毕') self.say() def echo(self): print(self.__Country) class SheDiao(Person): def __init__(self,name,hobby): super(SheDiao, self).__init__(name,hobby) print('SheDiao子类对象创建完毕') def echo(self): print(self.__skill) if __name__ =="__main__": p1 = TianLong('乔峰','喝酒') p2 = SheDiao('乔峰','喝酒') p1.echo() p2.echo() # 会报错 'SheDiao' object has no attribute '_SheDiao__skill'
TianLong子类对象创建完毕 Traceback (most recent call last): File "D:/py3script/python66/object/13_1_1.py", line 37, in <module> p1 = TianLong('乔峰','喝酒') File "D:/py3script/python66/object/13_1_1.py", line 20, in __init__ self.say() AttributeError: 'TianLong' object has no attribute 'say'
很赞哦!
python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群
相关文章
文章评论
-
私有属性私有方法在子类中也不可以访问文章写得不错,值得赞赏