继承的概念
生活中的继承,一般指的是子女继承父辈的财产
如果类名后面的括号当中不写任何内容,那他就默认继承object(基类)
python面向对象的继承指的是多个类之间的所属关系,即子类默认继承父类的所有属性和方法,具体如下
在python中,所有类默认继承object类,object类是顶级类或基类;其他子类叫派生类
单继承
class Teacher(object): def __init__(self): self._name = "杨大哥" self.kunfu = "龟派气功" def teacher_yangdage(self): print(f"我是{self._name},我会{self.kunfu}") # 学生继承老师 class Student(Teacher): pass
如果父类当中有__init__,那么父类继承之后无须在写init
如何调用父类的属性和方法
class Teacher(object): def __init__(self): self._name = "杨大哥" self.kunfu = "龟派气功" def teacher_yangdage(self): print(f"我是{self._name},我会{self.kunfu}") # 学生继承老师 class Student(Teacher): pass if __name__ == '__main__': student = Student() print(student.kunfu)
私有方法和属性不能被调用
多继承
class Student(Teacher, TeacherSecond): pass if __name__ == '__main__': student = Student() print(student.kunfu)
这种只能够继承写在第一位的Teacher
如果要继承两个父类就如下图代码:
class Student(Teacher, TeacherSecond): def __init__(self): Teacher.__init__(self) TeacherSecond.__init__(self) if __name__ == '__main__': student = Student() print(student.kunfu1) print(student.kunfu2)
子类重写父类方法和属性
修改父类属性
class Teacher(object): def __init__(self): self._name = "杨大哥" self.kunfu1 = "龟派气功" def teacher_yangdage(self): print(f"我是{self._name},我会{self.kunfu}") class TeacherSecond(object): def __init__(self): self.name = "马保国" self.kunfu2 = "接化发" # 学生继承老师 class Student(Teacher, TeacherSecond): def __init__(self): Teacher.__init__(self) TeacherSecond.__init__(self) self.kunfu1="激光射线" self.kunfu2="如来神掌" def student_kunfu(self): print(f"学生自创的功夫:{self.kunfu1}和{self.kunfu2}") if __name__ == '__main__': student = Student() student.student_kunfu()
修改父类属性过后,如果还想调用父类原本的属性
class Student(Teacher, TeacherSecond): def __init__(self): Teacher.__init__(self) TeacherSecond.__init__(self) self.kunfu1="激光射线" self.kunfu2="如来神掌" def student_kunfu(self): print(f"学生自创的功夫:{self.kunfu1}和{self.kunfu2}") def yangdage(self): Teacher.__init__(self) print(self.kunfu1) def mabaoguo(self): TeacherSecond.__init__(self) print(self.kunfu2)
多层继承
class Teacher(object): def __init__(self): self._name = "杨大哥" self.kunfu1 = "龟派气功" def teacher_yangdage(self): print(f"我是{self._name},我会{self.kunfu}") class TeacherSecond(object): def __init__(self): self.name = "马保国" self.kunfu2 = "接化发" # 学生继承老师 class StudentFirst(Teacher, TeacherSecond): def __init__(self): Teacher.__init__(self) TeacherSecond.__init__(self) self.kunfu1="激光射线" self.kunfu2="如来神掌" def student_kunfu(self): print(f"学生自创的功夫:{self.kunfu1}和{self.kunfu2}") def yangdage(self): Teacher.__init__(self) print(self.kunfu1) def mabaoguo(self): TeacherSecond.__init__(self) print(self.kunfu2) class StudentSecond(StudentFirst): pass if __name__ == '__main__': student =StudentSecond() print(student.kunfu2) print(student.kunfu1) student.student_kunfu() student.yangdage() student.mabaoguo()
super()查找父类当中的方法
运用在单继承当中
class Teacher(object): def __init__(self): self._name = "杨大哥" self.kunfu1 = "龟派气功" class StudentFirst(Teacher): def __init__(self): Teacher.__init__(self) super().__init__() self.kunfu1 = "六脉神剑" if __name__ == '__main__': student=StudentFirst() print(student.kunfu1)
!!!
Teacher.__init__(self) super().__init__()
这两种方法都非常相似,都是调用父类的方法
多态
传入不同的对象,产生不同的结果
class Father(object): def func(self): print("下达命令,出兵剿匪") class SonOne(Father): def func(self): print("正面迎敌") class SonTwo(Father): def func(self): print("背后偷袭") class Main(object): def func_demo(self, demo): demo.func() if __name__ == '__main__': demo1=SonOne() demo2=SonTwo() func=Main() func.func_demo(demo1)