首页 > 技术文章 > 92.私有属性和私有方法

kangwenju 2020-05-11 21:18 原文

私有权限

面向对象三大特性:封装、继承、多态

封装的意义:

  1. 将属性和方法放到一起做为一个整体,然后通过实例化对象来处理;
  2. 隐藏内部实现细节,只需要和对象及其属性和方法交互就可以了;
  3. 对类的属性和方法增加 访问权限控制。

私有权限:在属性名和方法名 前面 加上两个下划线 __

  1. 类的私有属性 和 私有方法,都不能通过对象直接访问,但是可以在本类内部访问;
  2. 类的私有属性 和 私有方法,都不会被子类继承,子类也无法访问;
  3. 私有属性 和 私有方法 往往用来处理类的内部事情,不通过对象处理,起到安全作用。
class Master(object):
    def __init__(self):
        self.kongfu = "古法煎饼果子配方" 
    def make_cake(self):          
        print("[古法] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu)

class School(object):
    def __init__(self):
        self.kongfu = "现代煎饼果子配方"

    def make_cake(self):
        print("[现代] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu)

class Prentice(School, Master):
    def __init__(self):
        self.kongfu = "猫氏煎饼果子配方"
        # 私有属性,可以在类内部通过self调用,但不能通过对象访问
        self.__money = 10000  

    # 私有方法,可以在类内部通过self调用,但不能通过对象访问
    def __print_info(self):
        print(self.kongfu)
        print(self.__money)

    def make_cake(self):
        self.__init__()
        print("[猫氏] 按照 <%s> 制作了一份煎饼果子..." % self.kongfu)

    def make_old_cake(self):
        Master.__init__(self) 
        Master.make_cake(self)


    def make_new_cake(self):
        School.__init__(self) 
        School.make_cake(self)

class PrenticePrentice(Prentice):
    pass


damao = Prentice()
# 对象不能访问私有权限的属性和方法
# print(damao.__money)
# damao.__print_info()


pp = PrenticePrentice()
# 子类不能继承父类私有权限的属性和方法
print(pp.__money) 
pp.__print_info()

总结

  • Python中没有像C++中 public 和 private 这些关键字来区别公有属性和私有属性。
  • Python是以属性命名方式来区分,如果在属性和方法名前面加了2个下划线'__',则表明该属性和方法是私有权限,否则为公有权限。

例子:

# 如果一个属性或者方法 以两个下划线开头 标识着是一个私有的属性和方法
# 保证的数据安全

# 自定义古法师傅类
class Master(object):

    def __init__(self):
        # 公有属性(无论在类的外部 还是在类的内部都可以正常的使用)
        self.kongfu = "古法配方"
        # 私有属性(只能在类的内部使用self访问 不能再类的外部使用对象进行访问)
        self.__money = 10000

    def func(self):
        print("钱:%d" % self.__money)
        self.__hello_python()

    # 公有方法
    def make_cake(self):
        print("按照<%s>制作煎饼果子" % self.kongfu)

    # 私有方法
    def __hello_python(self):
        print("你好python")

# 实际开发中 有些属性不想在类的外面让对象调用 可以把这属性进行私有
lsf = Master()
print(lsf.kongfu)
# print(lsf.__money)
lsf.func()
lsf.make_cake()

 私有属性和私有方法(继承):

# 自定义古法师傅类
class Master(object):

    def __init__(self):
        self.kongfu = "古法配方"
        self.__money = 10000

    def make_cake(self):
        print("按照<%s>制作煎饼果子" % self.kongfu)

    def __hello_python(self):
        print("你好python")

# 自定义一个徒弟类
class Prentice(Master):
    def func(self):
        pass
# 子类继承了父类
# 其实子类只是继承父类的公有方法和属性
# 对象
dm = Prentice()
print(dm.kongfu)
dm.make_cake()
# 官网不建议使用
# 官方推荐一切靠自觉
# print(dm._Master__money)
# 内置函数 查看一个对象有哪些属性和方法
print(dir(dm))
# lsf = Master()
# print(dir(lsf))

 

推荐阅读