python学习目录-17-私有化属性继承

私有属性的继承

  #私有属性和方法,子类不会继承

    class Person(Animal):
        def __demo(self):
            print("我是私有方法")

p._Person__demo() #自己类里定义的私有方法 使用  对象._类__私有方法名() 调用

私有化属性的调用

class Person:
    def __init__(self):
        self.__money = 200
        self.name = "coco"

    def show1(self):
        print(self.name, self.__money)


class Student(Person):
    def __init__(self):
        super().__init__()

        self.__money = 500

    def show(self):
        print("money:", self.__money)


s = Student()
s.show()  # 500
s.show1()  # 200
posted @ 2022-08-19 18:28  测试圈的彭于晏  阅读(103)  评论(0)    收藏  举报