• 【python】类的继承和super关键字


    通过继承创建的新类称为“子类”或“派生类”。

    被继承的类称为“基类”、“父类”或“超类”。

    一个子类可以继承多个基类。但是一般情况下,一个子类只能有一个基类,要实现多重继承,可以通过多级继承来实现

    继承概念的实现方式主要有2类:实现继承、接口继承。

    Ø         实现继承是指使用基类的属性和方法而无需额外编码的能力;
    Ø         接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力(子类重构爹类方法)
    单继承:
     
    class Person(object):
        def __init__(self, name,age):
            self.name = name
            self.age = age
    
        def eat(self):
            print("person is eating")
            self.num = 10
    class Man(Person):
        def __init__(self, name, age,money):
            super(Man,self).__init__(name, age)  ##使用super继承,等于 Person.__init__(self,name,age)
            self.money=money
    多继承
    Python2 经典类是按照深度优先继承的,新式类是按照广度优先继承的
    Python3 经典类和新式类都是按照广度优先来继承
    当继承多个父类时,多个父类构造函数的参数个数必须相同,否则会报错。
    class animal(object):
        def __init__(self,name,age):
            self.name=name
            self.age=age
    
        def eat(self):
            print("animal is eating")
            self.num = 5
    
    class person(object):
        def __init__(self, name,age):
            self.name = name
            self.age = age
    
        def eat(self):
            print("person is eating")
            self.num = 10
    class Man(person,animal):
        def __init__(self, name, age,money): ###python3 init时会按照广度优先
            super(Man,self).__init__(name, age)  ##使用super多继承
            self.money=money
    
        ###子类复写父类方法
        def eat(self):
            super().eat()  ##对父类重写时,只能通过super方法来调用父类的方法。正在重写时,无法通过self调用
            self.num+=3
        ###子类调用父类方法
        def big_eat(self):
            self.eat()  ##使用self调用子类的方法,如果子类重写了父类的方法,会调用重写后的方法,没有的话就调用父类的方法。
    
    
    m1=Man("paul",'22','500')
    m1.big_eat()
    #>>>person is eating
    print(m1.num)
    #>>13
    

      

    综合练习:

    class SchoolMember(object):
        members = 0  # 初始学校人数为0
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def tell(self):
            pass
    
        def enroll(self):
            '''注册'''
            SchoolMember.members += 1
            print("33[32;1mnew member [%s] is enrolled,now there are [%s] members.33[0m " % (
            self.name, SchoolMember.members))
    
        def __del__(self):
            '''析构方法'''
            print("33[31;1mmember [%s] is dead!33[0m" % self.name)
    
    
    class Teacher(SchoolMember):
        def __init__(self, name, age, course, salary):
            super(Teacher, self).__init__(name, age)
            self.course = course
            self.salary = salary
            self.enroll()
    
        def teaching(self):
            '''讲课方法'''
            print("Teacher [%s] is teaching [%s] for class [%s]" % (self.name, self.course, 's12'))
    
        def tell(self):
            '''自我介绍方法'''
            msg = '''Hi, my name is [%s], works for [%s] as a [%s] teacher !''' % (self.name, 'Oldboy', self.course)
            print(msg)
    
    
    class Student(SchoolMember):
        def __init__(self, name, age, grade, sid):
            super(Student, self).__init__(name, age)
            self.grade = grade
            self.sid = sid
            self.enroll()
    
        def tell(self):
            '''自我介绍方法'''
            msg = '''Hi, my name is [%s], I'm studying [%s] in [%s]!''' % (self.name, self.grade, 'Oldboy')
            print(msg)
    
    
    if __name__ == '__main__':
        t1 = Teacher("Alex", 22, 'Python', 20000)
        t2 = Teacher("TengLan", 29, 'Linux', 3000)
    
        s1 = Student("Qinghua", 24, "Python S12", 1483)
        s2 = Student("SanJiang", 26, "Python S12", 1484)
    
        t1.teaching()
        t2.teaching()
        t1.tell()
    

      

  • 相关阅读:
    六个火柴搭四个三角形
    网络修复
    210. nginx root && alias区别
    206.pythondocx 操作word
    212. go语言堆实现
    211. django中的static 静态文件配置
    Java单元测试实战
    多测师肖老师__rf之变量(26.5)
    多测师肖老师__rf框架之定位方法(26.4)
    Failed to install texlive.infra
  • 原文地址:https://www.cnblogs.com/paulwinflo/p/5826536.html
Copyright © 2020-2023  润新知