zoukankan      html  css  js  c++  java
  • python--继承

    继承

    继承是代码的重用

    • 子类中即实现父类的方法,又实现自己的方法:
    • super().父类方法名(参数(自传self))
    • 父类名.父类方法名(参数)
    • 子类以及子类实例化的对象 可以访问父类的任何方法或变量.
    class People():
        def __init__(self, name, age):
            self.Name = name
            self.Age = age
    
        def eat(self):
            print('%s is eating...' % self.Name)
    
        def talk(self):
            print('%s is talking...' % self.Name)
    
        def sleep(self):
            print('%s is sleeping...' % self.Name)
    
    
    class Man(People):  # 继承了People类,括号里面的 父类,基类,超类   括号外面的 子类,派生类.
    
        def sleep(self):  # 有相同的方法时调用自己的
            People.sleep(self)  # 调用了People的sleep方法
            super().sleep()  # 新式类的写法
            print('man is sleep...')
    
    
    m1 = Man('jack', 24)
    m1.eat()
    m1.sleep()

    结果:

    jack is eating...
    jack is sleeping...
    jack is sleeping...
    man is sleep...

    # class People():#经典类
    class People(object):  # 新式类
        def __init__(self, name, age):
            self.Name = name
            self.Age = age
    
        def eat(self):
            print('%s is eating...' % self.Name)
    
        def talk(self):
            print('%s is talking...' % self.Name)
    
        def sleep(self):
            print('%s is sleeping...' % self.Name)
    
    
    class Man(People):  # 继承了People类
        def __init__(self, name, age, money):  # 必须将参数都写上
            # People.__init__(self,name,age)  # name和age用People的实例变量,经典类写法
    
            super(Man, self).__init__(name, age)  # name和age用People的实例变量,新式类写法
    
            self.money = money
            print('%s have %s' % (self.Name, self.money))
    
    
    def sleep(self):  # 重构People类,有相同的方法时调用自己的
        People.sleep(self)  # 调用了People的方法
        print('man is sleep...')
    
    
    class Woman(People):
        def birth(self):
            print('woman is birth...')
    
    
    m1 = Man('jack', 24, 88888)
    m1.eat()
    m1.sleep()
    
    m2 = Woman('bob', 22)
    m2.birth()
    m2.sleep()

    结果:

    jack have 88888
    jack is eating...
    jack is sleeping...
    woman is birth...
    bob is sleeping...

    多继承

    class People(object):  # 新式类
        def __init__(self, name, age):
            self.name = name
            self.Age = age
    
        def eat(self):
            print('%s is eating...' % self.name)
    
        def talk(self):
            print('%s is talking...' % self.name)
    
        def sleep(self):
            print('%s is sleeping...' % self.name)
    
    
    class Friend(object):
        def make_friend(self, obj):  # 这里的obj为后面实例化的值,底下将 m1 实例化后传给了 obj
            print("%s is with %s" %(self.name, obj.name))  # obj.name=jack
    
    
    class Man(People, Friend):  # 继承了People类
        def __init__(self, name, age, money):  # 必须将参数都写上
            # People.__init__(self,name,age)  # name和age用People的实例变量,经典类写法
            super(Man, self).__init__(name, age)  # name和age用People的实例变量,新式类写法
            self.money = money
    
        def sleep(self):  # 重构People类,有相同的方法时调用自己的
            People.sleep(self)  # 调用了People的方法
            print('man is sleep...')
    
    
    class Woman(People, Friend):
        def birth(self):
            print('woman is birth...')
    
    
    m1 = Man('jack', 24, 88888)
    print(m1)
    m2 = Woman('bob', 22)
    m2.make_friend(m1)  # make_friend需要传一个值,将m1传给   make_friend

    结果:

    <__main__.Man object at 0x0320B430>
    bob is with jack

    Friend不用初始化,因为先继承people,里面有name,age,如果people和Friend换位置,也没事,因为Friend没有执行

    类名.mro()查看类的继承顺序

    class A:
        def __init__(self):
            print("A")
    
    
    class B(A):
        pass
        # def __init__(self):
        #     print("B")
    
    
    class C(A):
        # pass
        def __init__(self):
            print("C")
    
    
    class D(B, C):
        pass
        # def __init__(self):
        #     print("D")
    
    
    obj = D()

    结果:

    C

    py2经典类是按深度优先来继承的,新式类是按广度优先来继承的

    py3经典类和新式类统一是按广度优先来继承的

    class School(object):
        def __init__(self, name, addr):
            self.name = name
            self.addr = addr
            self.students = []  # 定义了列表,放学生
            self.staffs = []  # 定义了列表,放老师
    
        def enroll(self, stu_obj):  # 学生注册
            print('为学员%s 办理注册手续' % stu_obj.name)
            self.students.append(stu_obj)  # 将学生加到列表里
    
        def hire(self, staff_obj):  # 雇佣老师,staff_obj实例化,这里是Teacher类
            print('雇佣新员工%s' % staff_obj.name)  # staff_obj.name是Teacher里的name
            self.staffs.append(staff_obj)  # 加到老师列表里
    
    
    class SchoolMember(object):
        def __init__(self, name, age, sex):
            self.name = name
            self.age = age
            self.sex = sex
    
        def tell(self):
            pass
    
    
    class Teacher(SchoolMember):
        def __init__(self, name, age, sex, salary, course):  # 先覆盖父类的
    
            super(Teacher, self).__init__(name, age, sex)  # 继承父类的name, age, sex
    
            self.salary = salary  # 添加新的属性
            self.course = course
    
        def tell(self):
            print('''
                -------info of Teacher:%s-------
                Name:%s
                Age:%s
                Sex:%s
                Salary:%s
                Course:%s
                ''' % (self.name, self.name, self.age, self.sex, self.salary, self.course))
    
        def teach(self):
            print('%s is teaching course [%s]' % (self.name, self.course))
    
    
    class Student(SchoolMember):
        def __init__(self, name, age, sex, stu_id, grade):
            super(Student, self).__init__(name, age, sex)
            self.stu_id = stu_id
            self.grade = grade
    
        def tell(self):
            print('''  -------info of Student:%s-------
            Name:%s
            Age:%s
            Sex:%s
            Stu_id:%s
            Grade:%s
            ''' % (self.name, self.name, self.age, self.sex, self.stu_id, self.grade))
    
        def pay_tuition(self, amount):
            print('%s has paid tution $%s' % (self.name, amount))
    
    
    school = School('高科', '西安')  # 实例化了School类
    t1 = Teacher('lfj', 32, 'm', 3000, 'python')  # 实例化了Teacher类
    t2 = Teacher('zouzou', 22, 'm', 30000, 'linuxDevops')
    # 实例化了Teacher类
    s1 = Student('jack', 36, 'mm', 1001, 'python')  # 实例化了Student类
    s2 = Student('bob', 30, 'mm', 1002, 'linuxDevops')
    # 实例化了 Student类
    t1.tell()
    s1.tell()
    school.hire(t1)  # 将t1传给hire
    school.enroll(s1)  # 将实例化了的Student类school下的enroll
    school.enroll(s2)
    print(school.students)  # 打印出学生的内存地址
    print(school.staffs)  # 打印出老师的内存地址
    school.staffs[0].teach()  # 调用了Teacher下的teach
    for stu in school.students:
        stu.pay_tuition(5000)  # 调用了students下的pay_tuition

    结果:

                -------info of Teacher:lfj-------
                Name:lfj
                Age:32
                Sex:m
                Salary:3000
                Course:python
                
      -------info of Student:jack-------
            Name:jack
            Age:36
            Sex:mm
            Stu_id:1001
            Grade:python
            
    雇佣新员工lfj
    为学员jack 办理注册手续
    为学员bob 办理注册手续
    [<__main__.Student object at 0x02C9B790>, <__main__.Student object at 0x02C9B7B0>]
    [<__main__.Teacher object at 0x02C9B750>]
    lfj is teaching course [python]
    jack has paid tution $5000
    bob has paid tution $5000
  • 相关阅读:
    Palindrome Partitioning
    triangle
    Populating Next Right Pointers in Each Node(I and II)
    分苹果(网易)
    Flatten Binary Tree to Linked List
    Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
    iOS系统navigationBar背景色,文字颜色处理
    登录,注销
    ios 文字上下滚动效果Demo
    经常崩溃就是数组字典引起的
  • 原文地址:https://www.cnblogs.com/zouzou-busy/p/13021586.html
Copyright © 2011-2022 走看看