python 类 公有属性、私有属性、公有方法、私有方法
#!/usr/bin/env python# -*- encoding: utf-8 -*-"""@Introduce : python 类 公有属性、私有属性、公有方法、私有方法 公有:类中 类外 都可以调用 私有:类中可调用 类外不能调用@File : public_private.py@Time : /9/11 10:12@Author : 夏华东@Tel : 150 021 96021@Emile : 15002196021@@pip : pip install """class Test: publicVar = "公有变量" __privateVar = "私有变量" def __init__(self, publicArg, privateArg): self.publicArg = publicArg # 公有属性 self.__privateArg = privateArg # 私有属性 # 公有方法 def public(self): self.__private() # 私有方法,类中调用。类外不能调用 print("普通方法public") # 公有方法 def _public(self): self.publicArg += "内部修改" # 内部修改 print(self.publicArg) self.__privateArg += "内部修改" # 内部修改
如果觉得《python 类 公有属性 私有属性 公有方法 私有方法》对你有帮助,请点赞、收藏,并留下你的观点哦!