python--继承

简介: 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

相关文章
|
7月前
|
Python
Python中的继承:概念、用法与示例
Python中的继承:概念、用法与示例
70 0
|
7月前
|
Python
请简述Python中的继承、封装和多态的概念。
【2月更文挑战第24天】【2月更文挑战第82篇】请简述Python中的继承、封装和多态的概念。
44 1
|
5天前
|
算法 Python
Python多继承时子类如何调用指定父类
通过本文的介绍,希望您能够深入理解Python多继承时子类如何调用指定父类的方法,并在实际项目中灵活运用这些技巧,编写出高效且易维护的代码。
22 11
|
4天前
|
算法 Python
Python多继承时子类如何调用指定父类
通过本文的介绍,希望您能够深入理解Python多继承时子类如何调用指定父类的方法,并在实际项目中灵活运用这些技巧,编写出高效且易维护的代码。
21 1
|
4月前
|
算法 Python
python多继承的3C算法是什么?怎么用?
有很多地方都说python多继承的继承顺序,是按照深度遍历的方式,其实python多继承顺序的算法,不是严格意义上的深度遍历,而是基于深度遍历基础上优化出一种叫3C算法
|
4月前
|
Python
Python 中的不同类型继承
【8月更文挑战第24天】
54 0
|
4月前
|
算法 Python
Python 中的继承:强大的代码复用机制
【8月更文挑战第23天】
67 0
|
7月前
|
Python
Python中的面向对象编程与继承
本文将深入探讨Python中面向对象编程的核心概念,重点讨论继承的实现原理以及在实际开发中的应用。通过详细的示例和解释,读者将能够全面理解Python中继承的使用方式和优势,为提高代码的复用性和可维护性提供有效的技术支持。
|
6月前
|
Python
Python面向对象进阶:深入解析面向对象三要素——封装、继承与多态
Python面向对象进阶:深入解析面向对象三要素——封装、继承与多态
|
7月前
|
Python
Python进阶---面向对象 继承
Python进阶---面向对象 继承
30 1