私有权限-python

1.设置私有权限的方法:在属性名和方法名 前面 加上两个下划线_

class Master():
    def __init__(self):
        self.kongfu = "[五香煎饼果子配方]"
        self.__money = 2000
    def make_cake(self):
        print(f"运用{self.kongfu}")
        print(f"花费{self.__money}")
    def __info_print(self):
        print(self.kongfu)
        print(self.__money)
class __School():
    def __init__(self):
        self.kongfu = "[香辣煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}")
class Prentice(Master):
    def __init__(self):
        self.kongfu = "[独创煎饼果子配方]"
    def make_cake(self):
        self.__init__()
        print(f"运用{self.kongfu}")
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)


class Tusun(Prentice):
    def make_old_cake(self):

        super().__init__()
        super().make_cake()

if __name__ == '__main__':

    result = Tusun()
    r = Prentice()
    s = __School()

    r.make_master_cake()
    print(r.kongfu)
    print(s.kongfu)

二、获取和修改私有属性和方法

class Master():
    def __init__(self):
        self.kongfu = "[五香煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}")
        print(f"花费{self.__money}")
class School():
    def __init__(self):
        self.kongfu = "[香辣煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}")
class Prentice(Master,School):
    def __init__(self):
        self.kongfu = "[独创煎饼果子配方]"
        self.__money = 500
    def get_money(self):
        return self.__money
    def set_money(self,m):
        self.__money = m
    def __info_print(self):
        print(self.kongfu)
        print(self.__money)
    def make_cake(self):
        self.__init__()
        print(f"运用{self.kongfu}制作煎饼果子")
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)
    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

class Tusun(Prentice):
    pass


if __name__ == '__main__':
    s = Prentice()
    result = Tusun()
    print(s.get_money())
    s.set_money(200)
    print(s.get_money())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XuMeng_first

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值