class Master(object):
def __init__(self):
self.kongfu='上古秘法'
def make_cake(self):
print(f'运用{self.kongfu}打败黑魔仙')
class School(object):
def __init__(self):
self.kongfu='理论教学'
def make_cake(self):
print(f'将{self.kongfu}化为实践')
class Prentice(School,Master):
pass
Prentice1=Prentice()
Prentice1.make_cake()
#当一个类有多个父类的时候,默认是用第一个父类的同名属性和方法
print(Prentice.__mro__)
输出:
将理论教学化为实践
(<class '__main__.Prentice'>, <class '__main__.School'>, <class '__main__.Master'>, <class 'object'>)
#mro方法查看类的继承情况
#super关键字
class Master(object):
def __init__(self):
self.kongfu='上古秘法'
def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(Master):
def __init__(self):
self.kongfu='理论教学'
def make_cake(self):
print(f'将{self.kongfu}化为实践制作煎饼果子')
# super(School,self).__init__()
# super(School, self).make_cake()
#无参数super
super().__init__()
super().make_cake()
class Prentice(School):
def __init__(self):
self.kongfu='[独创上古秘法]'
def make_cake(self):
#如果是先调用了父类的属性和方法,父类的属性会覆盖子类属性
#所以在调用属性钱,先调用自己子类的初始化
#如果不加自己的初始化,kongfu属性值是上一次调用的init内的kongfu属性值
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)
def make_old_cake(self):
# super(Prentice,self).__init__()
# super(Prentice,self).make_cake()
#无参数super
super().__init__()
super().make_cake()
YYQX=Prentice()
YYQX.make_old_cake()
#定义私有属性和方法
#设置私有权限的方法:在属性名和方法名前面+两个下划线
class Master(object):
def __init__(self):
self.kongfu='上古秘法'
def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(Master):
def __init__(self):
self.kongfu='理论教学'
def make_cake(self):
print(f'将{self.kongfu}化为实践制作煎饼果子')
# super(School,self).__init__()
# super(School, self).make_cake()
#无参数super
super().__init__()
super().make_cake()
class YYQX(School):
def __init__(self):
self.kongfu='[独创上古秘法]'
#定义私有属性
self.__money=3000000
#定义私有方法
def __info_print(self):
print(self.kongfu)
print(self.__money)
def make_cake(self):
#如果是先调用了父类的属性和方法,父类的属性会覆盖子类属性
#所以在调用属性钱,先调用自己子类的初始化
#如果不加自己的初始化,kongfu属性值是上一次调用的init内的kongfu属性值
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)
def make_old_cake(self):
# super(Prentice,self).__init__()
# super(Prentice,self).make_cake()
#无参数super
super().__init__()
super().make_cake()
class NanNan(YYQX):
pass
YYQX1=YYQX()
#对象不能访问私有属性和私有方法
NanNan1=NanNan()
#子类无法继承父类的私有属性和私有方法
#在Python中,一般定义函数名get_xx用来获取私有属性,定义set_xx用来修改私有属性值
#私有属性和私有方法只能在类里面访问和修改
class Master(object):
def __init__(self):
self.kongfu='上古秘法'
def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(Master):
def __init__(self):
self.kongfu='理论教学'
def make_cake(self):
print(f'将{self.kongfu}化为实践制作煎饼果子')
# super(School,self).__init__()
# super(School, self).make_cake()
#无参数super
super().__init__()
super().make_cake()
class YYQX(School):
def __init__(self):
self.kongfu='[独创上古秘法]'
#定义私有属性
self.__money=3000000
#定义私有方法
def __info_print(self):
print(self.kongfu)
print(self.__money)
#获取私有属性
def get_money(self):
return self.__money
#修改私有属性
def set_money(self):
self.__money=500
def make_cake(self):
#如果是先调用了父类的属性和方法,父类的属性会覆盖子类属性
#所以在调用属性钱,先调用自己子类的初始化
#如果不加自己的初始化,kongfu属性值是上一次调用的init内的kongfu属性值
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)
def make_old_cake(self):
# super(Prentice,self).__init__()
# super(Prentice,self).make_cake()
#无参数super
super().__init__()
super().make_cake()
class NanNan(YYQX):
pass
YYQX1=YYQX()
#对象不能访问私有属性和私有方法
NanNan1=NanNan()
#调用get_money函数获取私有属性money的值
print(NanNan1.get_money())
#调用set_money函数修改私有属性money的值
NanNan1.set_money()
print(NanNan1.get_money())
输出:
3000000
500