继承
子类默认继承父类所有的属性和方法(私有属性)
在python中所有类默认继承object类,object是顶级类或基类,其他子类叫做派生类
"""
class 类名:
代码
class 类名(object):
代码
"""
class A(object):
def __init__(self):
self.num = 1
def info_print(self):
print(self.num)
class B(A):
pass
b = B()
b.info_print()
class Master(object):
def __init__(self):
self.kongfu = ['古法煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class Student(Master):
pass
ssc = Student()
print(ssc.kongfu)
ssc.info_print()
- 多继承:一个子继承多个父类,默认使用第一个父类的同名方法和同名属性
class Master(object):
def __init__(self):
self.kongfu = ['古法煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class School(object):
def __init__(self):
self.kongfu = ['现今煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class Student(School, Master):
pass
ssc = Student()
print(ssc.kongfu)
ssc.info_print()
- 当子类里面对父类同名的方法和属性时调用时优先调用子类自己的方法和属性
class Master(object):
def __init__(self):
self.kongfu = ['古法煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class School(object):
def __init__(self):
self.kongfu = ['现今煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class Student(School, Master):
def __init__(self):
self.kongfu = ['自创煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
ssc = Student()
print(ssc.kongfu)
ssc.info_print()
class Master(object):
def __init__(self):
self.kongfu = ['古法煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class School(object):
def __init__(self):
self.kongfu = ['现今煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class Student(School, Master):
def __init__(self):
self.kongfu = ['自创煎饼果子配方']
def info_print(self):
self.__init__(self)
print(f'运用{self.kongfu}做了个煎饼果子')
def Master_info_print(self):
Master.__init__(self)
Master.info_print(self)
def School_info_print(self):
School.__init__(self)
School.info_print(self)
ssc = Student()
print(ssc.kongfu)
ssc.info_print()
ssc.Master_info_print()
class Master(object):
def __init__(self):
self.kongfu = ['古法煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class School(object):
def __init__(self):
self.kongfu = ['现今煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class Student(School, Master):
def __init__(self):
self.kongfu = ['自创煎饼果子配方']
def info_print(self):
self.__init__()
print(f'运用{self.kongfu}做了个煎饼果子')
def Master_info_print(self):
Master.__init__(self)
Master.info_print(self)
def School_info_print(self):
School.__init__(self)
School.info_print(self)
class Tusun(Student):
pass
ssc = Tusun()
print(ssc.kongfu)
ssc.info_print()
ssc.Master_info_print()
print(Student.__mro__)
class Master(object):
def __init__(self):
self.kongfu = ['古法煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
class School(Master):
def __init__(self):
self.kongfu = ['现今煎饼果子配方']
def info_print(self):
print(f'运用{self.kongfu}做了个煎饼果子')
super().__init__()
super().info_print()
class Student(School):
def __init__(self):
self.kongfu = ['自创煎饼果子配方']
def info_print(self):
self.__init__()
print(f'运用{self.kongfu}做了个煎饼果子')
def Master_info_print(self):
super().__init__()
super().info_print()
ssc = Student()
ssc.Master_info_print()
在属性和方法前面添加_ _就代表私有属性和方法,不能继承给子类
def get_xx(self):
return self.__money
def set_xx(self):
self.__money = 500
|