文章目录
一、继承Ⅰ
class A():
def __init__(self):
self.num = 234
def info_print(self):
print(self.num)
class B(A):
pass
b = B()
print(b.info_print()) # 打印出来的234是括号里b.info_print()执行得到的,None是b.info_print()的地址被print出来了
print('======================')
b.info_print()
二、继承Ⅱ
# 定义师傅类
class Master():
def __init__(self):
self.kongfu = '咏春拳'
def make_cake(self):
print(f'用{self.kongfu}打擂台')
# 定义徒弟类
class Prentice(Master):
pass
# 用徒弟类创建对象,调用实例属性和方法
prentice = Prentice()
print(prentice.kongfu)
prentice.make_cake()
三、多继承
class Master(object):
def __init__(self):
self.kongfu = "九阴白骨爪"
def fight(self):
print(f'运用{self.kongfu}打遍天下无敌手')
class School(object):
def __init__(self):
self.kongfu = "降龙十八掌"
def fight(self):
print(f'一套{self.kongfu}战胜了九阴白骨爪')
class Prentice(School, Master): # 优先继承第一个类(写在最前面的)
pass
prentice = Prentice()
print(prentice.kongfu)
prentice.fight()
四、重写
class Master(object):
def __init__(self):
self.kongfu = "九阴白骨爪"
def fight(self):
print(f'运用{self.kongfu}打遍天下无敌手')
class School(object):
def __init__(self):
self.kongfu = "降龙十八掌"
def fight(self):
print(f'一套{self.kongfu}战胜了九阴白骨爪')
class Prentice(School, Master): # 优先继承第一个类(写在最前面的),重写父类的同名方法和同名属性
def __init__(self):
super().__init__()
self.kongfu = "进阶式九阴白骨爪"
def fight(self):
print(f'运用进阶式{self.kongfu}打败了九阴白骨爪和降龙十八掌')
prentice = Prentice()
print(prentice.kongfu)
prentice.fight()
print(Prentice.__mro__) # 快速打印出该类的继承关系
五、重写调用
class Master(object):
def __init__(self):
self.kongfu = "九阴白骨爪"
def fight(self):
print(f'展示{self.kongfu}打遍天下无敌手')
class School(object):
def __init__(self):
self.kongfu = "降龙十八掌"
def fight(self):
print(f'一套{self.kongfu}战胜了九阴白骨爪')
class Prentice(School, Master): # 优先继承第一个类(写在最前面的),重写父类的同名方法和同名属性
def __init__(self):
self.kongfu = "独孤九剑"
def fight(self):
self.__init__() # 如果是先调用了父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己类的初始化
print(f'使用{self.kongfu}打败了九阴白骨爪和降龙十八掌')
def prent_master(self): # 调用父类方法,但为保证调用到的也是父类的属性,必须在调用方法之前调用它的初始化
Master.__init__(self)
Master.fight(self)
def prent_school(self):
School.__init__(self)
School.fight(self)
prentice = Prentice()
print(prentice.kongfu)
prentice.fight()
prentice.prent_master()
prentice.prent_school()
prentice.fight()
六、多层继承
class Master(object):
def __init__(self):
self.kongfu = "九阴白骨爪"
def fight(self):
print(f'展示{self.kongfu}打遍天下无敌手')
class School(object):
def __init__(self):
self.kongfu = "降龙十八掌"
def fight(self):
print(f'一套{self.kongfu}战胜了九阴白骨爪')
class Prentice(School, Master): # 优先继承第一个类(写在最前面的),重写父类的同名方法和同名属性
def __init__(self):
self.kongfu = "独孤九剑"
def fight(self):
self.__init__() # 如果是先调用了父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己类的初始化
print(f'使用{self.kongfu}打败了九阴白骨爪和降龙十八掌')
def prent_master(self): # 调用父类方法,但为保证调用到的也是父类的属性,必须在调用方法之前调用它的初始化
Master.__init__(self)
Master.fight(self)
def prent_school(self):
School.__init__(self)
School.fight(self)
class Tusun(Prentice):
pass
tusun = Tusun()
print(tusun.kongfu)
tusun.fight()
tusun.prent_master()
tusun.prent_school()
print(Prentice.__mro__) # 快速打印出该类的继承关系
七、super方法
class Master(object):
def __init__(self):
self.kongfu = "爷爷类"
def fight(self):
print(f'这是{self.kongfu}方法')
class School(Master):
def __init__(self):
super().__init__()
self.kongfu = "父类"
def fight(self):
print(f'这是{self.kongfu}方法')
def father_class(self):
super(School, self).__init__()
super(School, self).fight()
def father_class02(self):
super().__init__()
super().fight()
class Prentice(School): # 优先继承第一个类(写在最前面的),重写父类的同名方法和同名属性
def __init__(self):
super().__init__()
self.kongfu = "子类"
def fight(self):
self.__init__() # 如果是先调用了父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己类的初始化
print(f'这是{self.kongfu}方法')
# 方法一:(在子类中调用父类中的属性和方法)
# 如果父类的名字修改了,则这里也必须修改
def father_class(self):
Master.__init__(self) # 调用父类方法,但为保证调用到属性的也是父类的属性,必须在调用方法之前调用它的初始化
Master.fight(self)
School.__init__(self)
School.fight(self)
# 方法二:(super(当前类名, self).方法)
def father_class02(self):
super(Prentice, self).__init__()
super(Prentice, self).fight()
# 方法三:
def father_class03(self):
super().__init__()
super().fight()
xiaoqiu = Prentice()
xiaoqiu.fight()
print("=========================================")
xiaoqiu.father_class03()
print("=========================================")
school = School()
school.father_class02()
八、私有权限
class Teacher(object):
def __init__(self):
self.course = "语文" # 前面没有下划线,说明是公有属性
self.__wage = 6000 # 前面有下划线,说明是私有属性
def skill(self):
print(f'父类是教{self.course}的')
def __info__salary(self): # 前面有下划线,说明是私有方法
print(f'老师的工资是{self.__wage}')
class Student(Teacher):
def __init__(self):
super().__init__() # 必须加这句代码,子类才能继承 course = "语文" 属性
self.subject = "数学和语文"
def skill(self):
print(f'子类是学{self.subject}的')
student = Student()
print(student.subject)
print(student.course)
print(student.__wage) # 私有属性,不能继承
student.__info__salary # 私有方法,不能继承
九、修改私有属性
class Teacher(object):
def __init__(self):
self.course = "语文" # 前面没有下划线,说明是公有属性
self.__wage = 6000 # 前面有下划线,说明是《私有属性》
def get_wage(self):
return self.__wage
def set_wage(self, num):
self.__wage = num
def __info__salary(self): # 前面有下划线,说明是《私有方法》
print(f'老师的工资是{self.__wage}')
def skill(self):
print(f'父类是教{self.course}的')
class Student(Teacher):
def __init__(self):
super().__init__() # 必须加这句代码,子类才能继承 course = "语文" 属性
self.subject = "数学和语文"
def skill(self):
print(f'子类是学{self.subject}的')
student = Student()
print(student.course)
student.skill()
print("=======================================")
teacher = Teacher()
print(f'获取当前老师的工资:{teacher.get_wage()}')
teacher.set_wage(5200)
print(f'获取修改后的老师的工资:{teacher.get_wage()}')
十、类方法
class Abc(object):
__tooth = 10 # 两条下划线私有类属性
@classmethod # 类方法和类属性一起使用,这里是用来获取私有类属性的值
def get_tooth(cls):
return cls.__tooth
abc = Abc()
print(abc.get_tooth())
十一、静态方法
class Dog(object):
@staticmethod # 定义静态方法,
def info_print():
print("这是一个静态方法")
dog = Dog()
print(dog.info_print()) # 通过创建对象来调用静态方法
print(Dog.info_print()) # 直接通过类来调用静态方法