面向对象—继承、私有属性

1、单继承

# class A:
#     def __init__(self,num):
#         self.num=num
#     def __str__(self):
#         return f"{self.num}的值"
#
# b=A(10)
# print(b)
""""体验继承"""
#定义子类 定义父类 创建对象 验证
# class A(object):
#     def __init__(self):
#         self.num=1
#     def info_print(self):
#         print(self.num)
#
# class B(A):
#     pass
#
# result=B()
# result.info_print()


class shifu(object):
    def __init__(self):
        self.gongfu='古法煎饼果子'
    def tudi1(self):
        print(f"{self.gongfu}")
class tudi(shifu):
    pass
tu=tudi()
tu.tudi1()

2、多继承

class shifu(object):
    def __init__(self):
        self.gongfu = '古法煎饼果子'

    def pro_info(self):
        print(f"{self.gongfu}是")


class School(object):
    def __init__(self):
        self.gongfu = "新式煎饼果子"

    def pro_info(self):
        print(f"{self.gongfu}")


class prentice(shifu, School):
    # def __init__(self):
    #     self.gongfu = "自己的独创方法"
    #
    # def pro_info(self):
    #     self.__init__()  # 加上自己的初始化  如果不加这个初始化 会导致gongfu值是上一个init调用的gongfu值
    #     print(f"自己的:{self.gongfu}")

    # 字类调用父类同名属性和方法 需要把父类的属性和方法再次封装成函数进行调用
    def mark_shifu(self):
        # 父类类名.函数()
        # 再次调用初始化的原因,想要再次调用父类的同名属性和方法,属性在init位置,所以再次调用
        shifu.__init__(self)
        shifu.pro_info(self)

    def mark_scaool(self):
        School.__init__(self)
        School.pro_info(self)
class Tusun(prentice):
    pass

TUzi = Tusun()
TUzi.mark_shifu()
# TUzi.mark_shifu()
TUzi.mark_scaool()

print(prentice.__mro__)
print(Tusun.__mro__)

3、字类调用父类同名方法

class shifu(object):
    def __init__(self):
        self.gongfu='古法煎饼果子'
    def pro_info(self):
        print(f"{self.gongfu}是")

class School(object):
    def __init__(self):
        self.gongfu="新式煎饼果子"
    def pro_info(self):
        print(f"{self.gongfu}")

class prentice(shifu,School):
    def __init__(self):
        self.gongfu="自己的独创方法"
    def pro_info(self):
        self.__init__() #加上自己的初始化  如果不加这个初始化 会导致gongfu值是上一个init调用的gongfu值
        print(f"自己的:{self.gongfu}")
#字类调用父类同名属性和方法 需要把父类的属性和方法再次封装成函数进行调用
    def mark_shifu(self):
        #父类类名.函数()
        #再次调用初始化的原因,想要再次调用父类的同名属性和方法,属性在init位置,所以再次调用
        shifu.__init__(self)
        shifu.pro_info(self)
    def mark_scaool(self):
        School.__init__(self)
        School.pro_info(self)

ziji=prentice()
ziji.pro_info()
ziji.mark_shifu()
ziji.mark_scaool()
ziji.pro_info()  #注意甄别需要初始化 添加自己的init 初始化init值

4、字类重写父类属性和方法

class shifu(object):
    def __init__(self):
        self.gongfu='古法煎饼果子'
    def pro_info(self):
        print(f"{self.gongfu}是")

class School(object):
    def __init__(self):
        self.gongfu="新式煎饼果子"
    def pro_info(self):
        print(f"{self.gongfu}")

class prentice(shifu,School):
    def __init__(self):
        self.gongfu="自己的独创方法"
    def pro_info(self):
        print(f"自己的:{self.gongfu}")

ziji=prentice()
print(ziji.gongfu)
ziji.pro_info()

5、私有属性和方法

class shifu(object):
    def __init__(self):
        self.gongfu='古法煎饼果子'

    def pro_info(self):
        print(f"{self.gongfu}是")
class School(object):
    def __init__(self):
        self.gongfu="新式煎饼果子"
        self.aaaa="王八"
        self.qwe = 1234
    def pro_info(self):
        print(f"{self.gongfu}")
        print(self.aaaa)

class prentice(shifu,School):
    def __init__(self):
        self.gongfu="自己的独创方法"
        self.__meney=1000  #私有属性
        self.qw=12345
    def aaaa(self):
        self.__qq=1233
    #定义私有方法
    def __aaaa(self):
        self.iqq=100
    def aaaaaa(self):
        #self.qw=1234
        pass
    def get_info(self):
        #self.__meney=123
        print(f"自己的:{self.__meney}")
        School.__init__(self)
        print(self.qwe)
a=prentice()
a.get_info()

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

class shifu(object):
    def __init__(self):
        self.gongfu='古法煎饼果子'

    def pro_info(self):
        print(f"{self.gongfu}是")
class School(object):
    def __init__(self):
        self.gongfu="新式煎饼果子"
    def pro_info(self):
        print(f"{self.gongfu}")

class prentice(shifu,School):
    def __init__(self):
        self.gongfu="自己的独创方法"
        self.__meney=1000  #私有属性
        """私有属性和私有方法只能在类里面进行访问和修改
        一般get__函数名 用来访问
            set__函数名用来修改"""
    #get__money
    def get__money(self):
        return  self.__meney
    def set__meney(self):
        self.__meney=123
        #print(f"自己的:{self.__meney}")

Tusun=prentice()
print(Tusun.get__money())
Tusun.set__meney()
print(Tusun.get__money())



"""私有方法可以通过如下方法调用被调用"""
class Example(object):
    def __init__(self, data1, data2):
        self.__data1 = data1
        self.data2 = data2

    def __func1(self):
        #self.__aa=123
        print("Example类的私有方法可以被调用!")

    def show_data(self):
        self.__func1()
        #print(self.__aa)
        print(self.__data1)
        print(self.data2)

exp = Example(50, 100)
exp.show_data()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值