8.27 (day24) inherited introduction, attributes the problem to find the diamond, special features are two ways to reuse inherit the parent class, binding method, mro list

review

# 类与对象
# class Student:
#     school = 'oldboy'
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
#     def study(self):
#         self.degree = 'benke'
#     def change_name(self,new_name):
#         self.name = new_name
#     def wife(self,obj):
#         self.my_wife = obj
#
# class Person:
#     school = 'oldboy'
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
#
# stu1 = Student('nick',48)
# 调用类类名():实例化产生一个空对象,去调用__init__方法,完成
# 对象的初始化
# stu1.sex = 'male'
# # print(stu1.sex)
# stu1.study()
# print(stu1.degree)
#
# stu1.change_name('wangredan')
# print(stu1.name)
#
# wife = Person('erya',18)
# stu1.wife(wife)
# print(stu1.my_wife.age)


# 对象有:数据属性(变量),方法属性(方法)
# 对象中可以嵌套对象
# 往对象中放属性的方式
# __init__   对象.属性   对象.方法(在方法中放属性)
# 属性查找顺序:先从对象中招,去类中招,没了报错
# __init__:类加括号完成实例化,会自动调用
# 绑定方法:类来调用普通函数,有几个形参传几个实参
# 对象调用时,第一个可以不用传
# 一切皆对象

Class inheritance

# 类的继承:
# IDE集成开发环境
# 继承介绍
# 什么是继承:继承是一种新建类的方式,继承了一个类,类中的属性和方法
# 就在子类中
# 父类/基类   子类/派生类
# 新式类:只要继承了object的类就是新式类
# python3中默认继承object
# 经典类:没有继承object的类就是经典类
# python2需要显示指定继承object类
# python3中没有经典类,python2才有经典类

# class A:
#     pass
# class B(A):
#     pass
# __xxxx__类的内置属性
# B.__name__
# print(B.__name__ )
# print(B.__dict__)
# 查看父类
# 类名.__bases__
# 多继承
# class A(B,C):

Use inherited reduce code redundancy

# class Person:
#     school='oldboy'   #减少了代码量
# class Teacher(Person):
#
#     def __init__(self,name,age,level):
#         self.name=name
#         self.age=age
#         self.level=level
#
# class Student(Person):
#
#     def __init__(self,name,age,course):
#         self.name=name
#         self.age=age
#         self.course=course
#
# stu1=Student('wgj',20,'python')
# # stu1.school='xxx'
# print(stu1.school)

Order attribute lookup (diamond problem)

# 2.属性的查找顺序
# 先找对象----类中找----父类中(多继承)----报错
# 3.如何重用父类的方法
# class Person:
#     school='oldboy'   #减少了代码量
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
#
# class Teacher(Person):
#     pass
# class Student(Person):
#     pass
# 类实例化会调用__init__ 如果类中没有,去父类中找
# stu1=Student('wgj',20)
# print(stu1.school)

# 多层继承
# class A:
#     a='A'
# class B(A):
    # a='B'
# class C():
#     pass
    # a='C'
# class D(C,A):
    # a='D'
    # pass
# d=D()
# print(d.a)

# 多继承
# class A:
#     a='A'
# class B():
#     a='B'
# class C():
#     # pass
#     a='C'
# class D(A,B,C):   #按括号中从左往右找
#     a='D'
#     # pass
# d=D()
# print(d.a)

# 多继承多层继承混合查找
# class A:
#     a='A'
# class B():
#     a='B'
# class C():
#     # pass
#     a='C'
# class D(A,B,C):   #按括号中从左往右找
#     a='D'
#     # pass
# class E:
#     a='E'
# class F:
#     a='F'
# class G(A,B,C,D,E,F):
#     a='G'
# d=G()
# print(d.a)
# 继承的菱形问题(显示的继承第一个类,不是object类):
# 新式类和经典类的顺序不一样
# 形成一个圈就是菱形
# 新式类查找顺序:广度优先,左侧开始往上找,找到菱形定点(不包括顶点)结束
# 继续下一个继承的父类找到菱形顶点结束(不包括顶点)
# 经典类查找顺序:(只有python2中才有)深度优先,左侧开始往上找,找到菱形顶点结束,
# mro查找列表,只在新式类中有
# print(类名.mro()) = print(类名.__mro__)

Inherit two ways to reuse the parent class

# 继承重用父类方法一:指名道姓的使用
# 跟继承没有关系
# class A:
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#
# class Person:
#     school='oldboy'   #减少了代码量
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#     def study(self):
#         print('study')
# class Teacher(Person):
#
#     def __init__(self,name,age,level):
#         A.__init__(self,name,age)
#         # self.name=name
#         # self.age=age
#         self.level=level
#
# class Student(Person):
#
#     def __init__(self,name,age,course):
#         # 重用父类的__init__方法
#         Person.__init__(self,name,age)
#         # self.name=name
#         # self.age=age
#         self.course=course
#     def study(self):
#         Person.study(self)
#         print(f'{self.name}正在学习')
# stu1=Student('wgj',20,'python')
# stu1.school='xxx'
# print(stu1.school)


# 继承重用父类方法二:super关键字
# super().__init__
# super(类名,对象).__init__  在py2中使用,py3中使用和上面一样
# class A:
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age

# class Person:
#     school='oldboy'   #减少了代码量
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#     def study(self):
#         print('study')
# class Teacher(Person):
#
#     def __init__(self,name,age,level):
#         A.__init__(self,name,age)
#         # self.name=name
#         # self.age=age
#         self.level=level

# class Student(Person):

    # def __init__(self,name,age,course):
        # 重用父类的__init__方法
        # Person.__init__(self,name,age)
        # self.name=name
        # self.age=age
        # super().__init__(name,age)    #按照mro列表拿到父类对象
        # 对象来调用绑定方法,不需要传递第一个参数(self) 经典类,新式类
    #     super(Student,self).__init__(name,age)   #经典类必须写
    #     self.course=course
    # def study(self):
    #     Person.study(self)
    #     super().study()
#         print(f'{self.name}正在学习')
# stu1=Student('wgj',20,'python')
# stu1.school='xxx'
# print(stu1.school)

#
# class Student:
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#     def study(self):
#         print(self.name)
#         print('study')
#     def change_name(obj,new_name):
#         print(f"原名{obj.name}")
#         obj.name=new_name
#         print(f"现名{new_name}")
# 类调用对象的绑定方法,(写在类中的函数,没加装饰器),有几个形参就传多少实参
# Student.__init__(123,'nick',18)
# 类实例化产生对象,会自动调用__init__完成初始化操作
# stu=Student('nick',18)
# 绑定方法会把对象本身当做self传入

# stu2=Student('tank',18)
# stu2.study()

# 修改学生姓名
# stu=Student('nick',18)
# 第一种
# print(stu.name)
# stu.name='tank'
# print(stu.name)
# 第二种
# stu.change_name('tank')
# print(stu.name)
# 第三种
# Student.change_name(stu,'tank')
# print(stu.name)
# 第四种
# 定义函数
# def change_name(obj,name):
#     obj.name=name
#     print(f"现名{name}")
# change_name(stu,'tank')


Guess you like

Origin www.cnblogs.com/jiann/p/11529108.html