#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
#
# 参考资料:
# 面向对象编程初步 - tonador - 博客园
# https://www.cnblogs.com/yujianbao/articles/6223482.html
#
# Python成长之路【第九篇】:Python基础之面向对象 - Mr_Albert - 博客园
# https://www.cnblogs.com/albert0924/p/8921709.html
#
# Python 学习 --day-16 - UMRzg - 博客园
# http://www.cnblogs.com/qinzheg/articles/9394420.html
#
# ------------------------------------------------------------
# ******************** day25-静态、组合、继承 *******************
# ******************** day25-静态、组合、继承 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# ------------------------------------------------------------
# # 1、对于类中的函数属性调用
# # # 实例调用函数属性,调用时类会自动传入参数self
# # # 类 调用函数属性,调用时类不会自动传入参数self,需要自己传入参数
# # #
# # # 需要注意的是,实例有数值属性,但是没有函数属性,它的函数调用都是对类函数调用
# # # 而类中是有函数属性 + 数值属性
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2、静态属性引入
# # #
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2.1、静态属性引入1
# # # 把特定功能封装成一个函数
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2.2、@property,静态属性引入2
# # # 把特定功能封装成一个函数,使用@property来修饰的时候,在调用上可以不用()就可以运行,
# # # 看起来像是一个数值属性
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2.3、@property,静态属性引入3
# # # 把特定功能封装成一个函数,使用@property来修饰的时候
# # # 看起来像是一个数值属性
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 3、通过实例化,对类的方法进行调用
# # # 通过实例化,对类的方法进行调用,这个过程中,实例化的作用只是桥梁
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 4、classmethod,"不" 通过实例化,对类的方法进行调用
# # # classmethod所修饰的函数A,实例仍然可以访问, 但是它与实例是没有任何的关系的,并且,函数A
# # # 只能访问类的属性而不可以访问实例的属性
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 5、classmethod,"不" 通过实例化,对类的方法进行调用
# # # 所修饰的函数,实例仍然可以访问
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 6、类中的staticmethod
# # # 被修饰的函数脱离于类、 实例的绑定
# # # 类中被类中的staticmethod的函数属性,是属于类的工具包,它对于类中的所有其他属性都是
# # # 不绑定的,也不与类中的实例绑定,这个时候,他就相当于一个普通的函数,仅仅是用来给类
# # # 特定的提供一些方法,但却不属于类的特征属性
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 7、组合
# # # 定义一个人的类,人有头,手,脚等数据属性,这几个属性又可以是通过一个类实例化的对象,这就是组合
# # #
# # # 用途:
# # # 1:做关联
# # # 2:小的组成大的
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 8、组合1
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 9、组合应用,给区域学校添加课程
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 10、组合应用,给区域学校添加课程2
# # 这个通过的是列表来实现,相对于上面那种,差了一些
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 10.1、组合应用,给区域学校添加课程2
# # 这个通过的是列表来实现,相对于上面那种,差了一些
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 10.2、组合应用,给区域学校添加课程2
# # 这个通过的是列表来实现,相对于上面那种,差了一些
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 11、类的继承--数值属性继承
# # #
# # # 类的继承跟现实生活中的父、子、孙子、重孙子、继承关系一样,父类又称为基类
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 12、类数值属性继承1
# # # 以下列例子为基础,数值属性查找:如果son中有数值属性,那么,就会使用自己的,如果没有,
# # # 就回去Dad类中找
# # # 如果是Dad类中有添加数值属性,Son是继承了Dad类的,如果本身没有的话,仍然能够去Dad类中查找
# # # 本质上,两者的数值属性是相互独立
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 13、类函数属性继承
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 14、类函数属性继承2
# # # Dad类与继承类Son中,如果Son中有自己的函数属性就用自己的
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 15、接口继承引入
# # # 所有的类都有一个共同的属性方式,这个时候,可以将他们提取出来,放到同一个类当中,作为一
# # # 个基类
# # # 这个属于:继承基类的方法,并且做出自己的改变或者扩展(代码重用)
# # # 但是意义并不是很大,甚至常常是有害的,因为它使得子类与基类出现强耦合;
# # # 因此少用
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 15.1、 @abc.abstractmethod, 接口继承
# # # 以下面为例子,所有类中的共同有的函数属性--write、read全部提取出来到,All_file类中
# # # 通过@abc.abstractmethod进行修饰,所有的继承的类必须要有这个函数
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 15.2、 接口继承中,继承者缺少被继承者要求的函数
# # # 继承者==son类,被继承者==parent类,如果继承过程中,parent类接口要求2个函数属性,
# # # son中有1个,会报错
# # # 下面例子,
# # # 被继承者 == All_file; 继承者 == Mem Directory
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 15.3、 接口继承中,继承者有被继承者要求的函数之外,还有其他函数属性
# # # 以下面为例子,所有类中的共同有的函数属性--write、read全部提取出来到,All_file类中
# # # 通过@abc.abstractmethod进行修饰,所有的继承的类必须要有这个函数,
# # # 但是,继承者在这个基础上可以有其他的函数
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 16、继承顺序引入( 新式类,广度优先)
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 16.1、继承顺序引入
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 16.2、继承顺序引入
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 16.3、继承顺序引入
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 16.4、继承顺序引入
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 16.5、继承顺序引入
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 16.6、总结:继承顺序引入
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# # # 这种方式称之为:广度优先!python3中的所有的类都是新式类,python2x中的是经典类
# # # 新式类:满足class A(object),即父类是object类_main__.A'>, <class 'object'>(见序号17中的print(F.__mro__) ),
# # # python3中都是新式类,因此 class A == class A(object)
# # # 经典类:????
# # # 当类是经典类时,多继承情况下,会按照深度优先方式查找
# # #
# # # Python成长之路【第九篇】:Python基础之面向对象 - Mr_Albert - 博客园
# # # https://www.cnblogs.com/albert0924/p/8921709.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 17、继承顺序的的确定
# # # Python到底是如何实现继承的,对于你定义的每一个类,Python会计算出一个方法
# # # 解析顺序(MRO)元组,这个MRO元组就是一个简单的所有基类的线性顺序列表
# # #
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 18、继承顺序引入( 经典类,深度优先)
# # # 经典类: class A; 新式类:class A(object)
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# # #
# # # 编译环境python2.7( 没有标注的都是python3)( 没有标注的都是python3)
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 18.1、继承顺序引入
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# # #
# # # 编译环境python2.7( 没有标注的都是python3)
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 18.2、继承顺序引入
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# # #
# # # 编译环境python2.7( 没有标注的都是python3)
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 18.3、继承顺序引入(与16.3对比)
# # # 深度优先,会使用分支一找到A
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# # #
# # # 编译环境python2.7( 没有标注的都是python3)
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 18.4、继承顺序引入(与16.3对比)
# # # class A: 经典类,深度优先; class A(object): 新式类,广度优先
# # # 广度优先,B找不到,回来通过E找,后面的同python3的情况
# # # 继承结构:分支一: F --> D --> B --> A;
# # # 分支二: F --> E --> C --> A;
# # #
# # # 编译环境python2.7
# ------------------------------------------------------------
# # 19、子类与父类中__init__有重复
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 20、关于self自动传入参数的情况:1、实例化; 2、对象调用类方法
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 21、子类与父类中__init__重复解决方式
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 22、子类调用父类的方法
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 23、super调用父类的方法
# # # =====>>>>>>上面的那种子类调用父类的方法还不够好
下面的1、2、3的表达方式是等价的
1、super().__init__(name, speed, load, power)
2、super(__class__,self).__init__(name,speed,load,power) # __class__是类名,见# print(line13.__class__)
3、super(Subway,self).__init__(name,speed,load,power)
# ------------------------------------------------------------
# ------------------------------------------------分割线------------------------------------------------- # ------------------------------------------------分割线------------------------------------------------- # ------------------------------------------------分割线-------------------------------------------------
# 01 上节课回顾 # 01 上节课回顾 ''' # ------------------------------------------------------------ # # 1、对于类中的函数属性调用 # # # 实例调用函数属性,调用时类会自动传入参数self # # # 类 调用函数属性,调用时类不会自动传入参数self,需要自己传入参数 # # # # # # 需要注意的是,实例有数值属性,但是没有函数属性,它的函数调用都是对类函数调用 # # # 而类中是有函数属性 + 数值属性 # ------------------------------------------------------------ ''' # # class School: # x = 1 # def __init__(self, name, addr, type): # self.Name = name # self.Addr = addr # self.Type = type # def tell_info(self): # print("学校的详细信息是: name: %s addr: %s" %(self.Name, self.Addr)) # # # s1 = School("青鸟", "神都", "私立") # print(s1.__dict__) # print(School.__dict__) # # s1.tell_info() # School.tell_info(s1) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # {'Name': '青鸟', 'Addr': '神都', 'Type': '私立'} # # {'__module__': '__main__', 'x': 1, '__init__': <function School.__init__ at 0x0000000002984620>, 'tell_info': <function School.tell_info at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'School' objects>, '__weakref__': <attribute '__weakref__' of 'School' objects>, '__doc__': None} # # 学校的详细信息是: name: 青鸟 addr: 神都 # # 学校的详细信息是: name: 青鸟 addr: 神都 # # # # Process finished with exit code 0 # 02 静态属性 # 02 静态属性 ''' # ------------------------------------------------------------ # # 2、静态属性引入 # # # # ------------------------------------------------------------ ''' # # class Room: # def __init__(self, name, owner, width, length, heigh): # self.name = name # self.owner= owner # self.width= width # self.length=length # self.heigh= heigh # # def cal_area(self): # print(" %s 住的 %s 总面积是 %s" % (self.owner, self.name, self.length * self.width)) # # # r1 = Room("一环", "tom", 100, 100, 999) # r2 = Room("0环", "zhang", 1, 1, 1) # # print(" %s 住的 %s 总面积是 %s" %(r1.owner, r1.name, r1.length*r1.width)) # print(" %s 住的 %s 总面积是 %s" %(r2.owner, r2.name, r2.length*r2.width)) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # tom 住的 一环 总面积是 10000 # # zhang 住的 0环 总面积是 1 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 2.1、静态属性引入1 # # # 把特定功能封装成一个函数 # ------------------------------------------------------------ ''' # # class Room: # def __init__(self, name, owner, width, length, heigh): # self.name = name # self.owner= owner # self.width= width # self.length=length # self.heigh= heigh # # def cal_area(self): # print(" %s 住的 %s 总面积是 %s" % (self.owner, self.name, self.length * self.width)) # # # r1 = Room("一环", "tom", 100, 100, 999) # r2 = Room("0环", "zhang", 1, 1, 1) # # r1.cal_area() # r2.cal_area() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # tom 住的 一环 总面积是 10000 # # zhang 住的 0环 总面积是 1 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 2.2、@property,静态属性引入2 # # # 把特定功能封装成一个函数,使用@property来修饰的时候,在调用上可以不用()就可以运行, # # # 看起来像是一个数值属性 # ------------------------------------------------------------ ''' # # class Room: # def __init__(self, name, owner, width, length, heigh): # self.name = name # self.owner= owner # self.width= width # self.length=length # self.heigh= heigh # @property # def cal_area(self): # print(" %s 住的 %s 总面积是 %s" % (self.owner, self.name, self.length * self.width)) # # # r1 = Room("一环", "tom", 100, 100, 999) # r2 = Room("0环", "zhang", 1, 1, 1) # # # 实际上是一个函数,但是看起来像是一个数值属性 # r1.cal_area # r2.cal_area # # # 数值属性 # print(r1.owner) # print(r2.owner) # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # tom 住的 一环 总面积是 10000 # # zhang 住的 0环 总面积是 1 # # tom # # zhang # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 2.3、@property,静态属性引入3 # # # 把特定功能封装成一个函数,使用@property来修饰的时候 # # # 看起来像是一个数值属性 # ------------------------------------------------------------ ''' # # class Room: # def __init__(self, name, owner, width, length, heigh): # self.name = name # self.owner= owner # self.width= width # self.length=length # self.heigh= heigh # @property # def cal_area(self): # # print(" %s 住的 %s 总面积是 %s" % (self.owner, self.name, self.length * self.width)) # return self.length * self.width # # r1 = Room("一环", "tom", 100, 100, 999) # r2 = Room("0环", "zhang", 1, 1, 1) # # # 实际上是一个函数,但是看起来像是一个数值属性 # print( r1.cal_area ) # print( r2.cal_area ) # # # 数值属性 # print(r1.owner) # print(r2.owner) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # 10000 # # 1 # # tom # # zhang # # # # Process finished with exit code 0 # 03 类方法 # 03 类方法 ''' # ------------------------------------------------------------ # # 3、通过实例化,对类的方法进行调用 # # # 通过实例化,对类的方法进行调用,这个过程中,实例化的作用只是桥梁 # ------------------------------------------------------------ ''' # # class Room: # tag = 1 # def __init__(self, name, owner, width, length, heigh): # self.name = name # self.owner= owner # self.width= width # self.length=length # self.heigh= heigh # @property # def cal_area(self): # # print(" %s 住的 %s 总面积是 %s" % (self.owner, self.name, self.length * self.width)) # return self.length * self.width # # def test(self): # print("from test", self.name) # # def tell_info(self): # print("====>>>>", self.tag) # # # r1 = Room("一环", "tom", 100, 100, 999) # print(Room.tag) # # Room.test(r1) # Room.test("99") # 报错, 没有通宵过实例,无法调用 # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # 1 # # from test 一环 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 4、classmethod,"不" 通过实例化,对类的方法进行调用 # # # classmethod所修饰的函数A,实例仍然可以访问, 但是它与实例是没有任何的关系的,并且,函数A # # # 只能访问类的属性而不可以访问实例的属性 # ------------------------------------------------------------ ''' # # class Room: # tag = 1 # def __init__(self, name, owner, width, length, heigh): # self.name = name # self.owner= owner # self.width= width # self.length=length # self.heigh= heigh # @property # def cal_area(self): # # print(" %s 住的 %s 总面积是 %s" % (self.owner, self.name, self.length * self.width)) # return self.length * self.width # # def test(self): # print("from test", self.name) # # @classmethod # 定义为一个类方法, # def tell_info(cls, x): # 注意,自会动传入的参数是cls, 这个是类本身 # print(cls) # print("====>>>>", cls.tag, x) # # print("====>>>>", self.tag) # 报错 # # print(Room.__dict__) # Room.tell_info("=_+") # # print("分割线 实例调用".center(100,"-")) # r1 = Room("一环", "tom", 100, 100, 999) # r1.tell_info(99) # print("dic 实例调用".center(100,"-")) # print(Room.__dict__) # print(r1.__dict__) # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # {'__module__': '__main__', 'tag': 1, '__init__': <function Room.__init__ at 0x00000000029A4620>, 'cal_area': <property object at 0x0000000001E98138>, 'test': <function Room.test at 0x00000000039EE840>, 'tell_info': <classmethod object at 0x00000000029B5AC8>, '__dict__': <attribute '__dict__' of 'Room' objects>, '__weakref__': <attribute '__weakref__' of 'Room' objects>, '__doc__': None} # # <class '__main__.Room'> # # ====>>>> 1 =_+ # # ---------------------------------------------分割线 实例调用---------------------------------------------- # # <class '__main__.Room'> # # ====>>>> 1 99 # # ---------------------------------------------dic 实例调用--------------------------------------------- # # {'__module__': '__main__', 'tag': 1, '__init__': <function Room.__init__ at 0x00000000029A4620>, 'cal_area': <property object at 0x0000000001E98138>, 'test': <function Room.test at 0x00000000039EE840>, 'tell_info': <classmethod object at 0x00000000029B5AC8>, '__dict__': <attribute '__dict__' of 'Room' objects>, '__weakref__': <attribute '__weakref__' of 'Room' objects>, '__doc__': None} # # {'name': '一环', 'owner': 'tom', 'width': 100, 'length': 100, 'heigh': 999} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 5、classmethod,"不" 通过实例化,对类的方法进行调用 # # # 所修饰的函数,实例仍然可以访问 # ------------------------------------------------------------ ''' # # class Room: # tag = 1 # def __init__(self, name, owner, width, length, heigh): # self.name = name # self.owner= owner # self.width= width # self.length=length # self.heigh= heigh # @classmethod # 定义为一个类方法, # def tell_info(cls): # 注意,自会动传入的参数是cls, 这个是类本身 # print(cls) # print("====>>>>", cls.tag) # # print("====>>>>", self.tag) # 报错 # # print(Room.__dict__) # Room.tell_info() # # print("分割线 实例调用".center(100,"-")) # r1 = Room("一环", "tom", 100, 100, 999) # r1.tell_info() # print("dic 实例调用".center(100,"-")) # print(Room.__dict__) # print(r1.__dict__) # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # {'__module__': '__main__', 'tag': 1, '__init__': <function Room.__init__ at 0x0000000002984620>, 'cal_area': <property object at 0x00000000027E8138>, 'test': <function Room.test at 0x00000000039EE840>, 'tell_info': <classmethod object at 0x0000000002995B70>, '__dict__': <attribute '__dict__' of 'Room' objects>, '__weakref__': <attribute '__weakref__' of 'Room' objects>, '__doc__': None} # # <class '__main__.Room'> # # ====>>>> 1 # # ---------------------------------------------分割线 实例调用---------------------------------------------- # # <class '__main__.Room'> # # ====>>>> 1 # # ---------------------------------------------dic 实例调用--------------------------------------------- # # {'__module__': '__main__', 'tag': 1, '__init__': <function Room.__init__ at 0x0000000002984620>, 'cal_area': <property object at 0x00000000027E8138>, 'test': <function Room.test at 0x00000000039EE840>, 'tell_info': <classmethod object at 0x0000000002995B70>, '__dict__': <attribute '__dict__' of 'Room' objects>, '__weakref__': <attribute '__weakref__' of 'Room' objects>, '__doc__': None} # # {'name': '一环', 'owner': 'tom', 'width': 100, 'length': 100, 'heigh': 999} # # # # Process finished with exit code 0 # 04 静态方法 # 04 静态方法 ''' # ------------------------------------------------------------ # # 6、类中的staticmethod # # # 被修饰的函数脱离于类、 实例的绑定 # # # 类中被类中的staticmethod的函数属性,是属于类的工具包,它对于类中的所有其他属性都是 # # # 不绑定的,也不与类中的实例绑定,这个时候,他就相当于一个普通的函数,仅仅是用来给类 # # # 特定的提供一些方法,但却不属于类的特征属性 # ------------------------------------------------------------ ''' # # class Room: # tag = 1 # def __init__(self, name, owner, width, length, heigh): # self.name = name # self.owner= owner # self.width= width # self.length=length # self.heigh= heigh # @staticmethod # def wash(a, b, c): # print("%s %s %s 正在洗东西" %(a, b, c )) # # print(tag) # 报错,找不到变量 # # print(self.tag) # 报错,找不到变量 # # print(cls.tag) # 报错,找不到变量 # print("wash中实现Room.tag: ", Room.tag) # 正确,通过类名来找 # # print("Room.__dict__: ",Room.__dict__) # Room.wash("AA","BB","ZHANG") # # # 实例可以调用staticmethod所修饰的函数属性 # print("分割线 实例调用".center(100,"-")) # r1 = Room("一环", "tom", 100, 100, 999) # r1.wash("r1一号", "二号", "三号") # print("dic".center(100,"-")) # print("Room.__dict__: ",Room.__dict__) # print("r1.__dict__: ", r1.__dict__) # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # Room.__dict__: {'__module__': '__main__', 'tag': 1, '__init__': <function Room.__init__ at 0x0000000002974620>, 'cal_area': <property object at 0x00000000021E8138>, 'test': <function Room.test at 0x00000000039EE840>, 'tell_info': <classmethod object at 0x00000000029912E8>, 'wash': <staticmethod object at 0x00000000039C6D68>, '__dict__': <attribute '__dict__' of 'Room' objects>, '__weakref__': <attribute '__weakref__' of 'Room' objects>, '__doc__': None} # # AA BB ZHANG 正在洗东西 # # ---------------------------------------------分割线 实例调用---------------------------------------------- # # r1一号 二号 三号 正在洗东西 # # ------------------------------------------------dic------------------------------------------------- # # Room.__dict__: {'__module__': '__main__', 'tag': 1, '__init__': <function Room.__init__ at 0x0000000002974620>, 'cal_area': <property object at 0x00000000021E8138>, 'test': <function Room.test at 0x00000000039EE840>, 'tell_info': <classmethod object at 0x00000000029912E8>, 'wash': <staticmethod object at 0x00000000039C6D68>, '__dict__': <attribute '__dict__' of 'Room' objects>, '__weakref__': <attribute '__weakref__' of 'Room' objects>, '__doc__': None} # # r1.__dict__: {'name': '一环', 'owner': 'tom', 'width': 100, 'length': 100, 'heigh': 999} # # # # Process finished with exit code 0 # 06 组合 # 06 组合 ''' # ------------------------------------------------------------ # # 7、组合 # # # 定义一个人的类,人有头,手,脚等数据属性,这几个属性又可以是通过一个类实例化的对象,这就是组合 # # # # # # 用途: # # # 1:做关联 # # # 2:小的组成大的 # ------------------------------------------------------------ ''' # # class Hand: # pass # # class Foot: # pass # # class Trunk: # pass # # class Head: # pass # # class Person: # def __init__(self, id_name, name): # self.id_num = id_name # self.name = name # self.hand = Hand() # self.foot = Foot() # self.trunk= Trunk() # self.head = Head() # # print("分割线".center(100,"-")) # print("Person.__dict__: ",Person.__dict__) # p1 = Person("999","alex") # # print("分割线".center(100,"-")) # print("Person.__dict__: ",Person.__dict__) # print("p1.__dict__: ", p1.__dict__) # # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # ------------------------------------------------分割线------------------------------------------------- # # Person.__dict__: {'__module__': '__main__', '__init__': <function Person.__init__ at 0x0000000002974620>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None} # # ------------------------------------------------分割线------------------------------------------------- # # Person.__dict__: {'__module__': '__main__', '__init__': <function Person.__init__ at 0x0000000002974620>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None} # # p1.__dict__: {'id_num': '999', 'name': 'alex', 'hand': <__main__.Hand object at 0x00000000029912B0>, 'foot': <__main__.Foot object at 0x00000000039DA438>, 'trunk': <__main__.Trunk object at 0x00000000039DA550>, 'head': <__main__.Head object at 0x00000000039DA390>} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 8、组合1 # # # # # # # # # # ------------------------------------------------------------ ''' # # class School: # def __init__(self, name ,addr): # self.name = name # self.addr = addr # # def zhao_sheng(self): # print("%s 正在招生" %self.name) # # class Course: # def __init__(self, name, price, period, school): # self.name = name # self.price = price # self.period = period # self.school = school # # s1 = School("oldboy", "北京") # s2 = School("oldboy", "南京") # s3 = School("oldboy", "东京") # # c1 = Course("linux", 10, '2h', " 北京") # c2 = Course("python", 33, '22h', s2) # # print(c1.__dict__) # print(c1.school) # print("分割线".center(100,"-")) # # print(c2.__dict__) # print(c2.school.name) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # {'name': 'linux', 'price': 10, 'period': '2h', 'school': ' 北京'} # # 北京 # # ------------------------------------------------分割线------------------------------------------------- # # {'name': 'python', 'price': 33, 'period': '22h', 'school': <__main__.School object at 0x0000000001E88E48>} # # oldboy # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 9、组合应用,给区域学校添加课程 # ------------------------------------------------------------ ''' # # class School: # def __init__(self, name ,addr): # self.name = name # self.addr = addr # # def zhao_sheng(self): # print("%s 正在招生" %self.name) # # class Course: # def __init__(self, name, price, period, school): # self.name = name # self.price = price # self.period = period # self.school = school # # s1 = School("oldboy", "北京") # s2 = School("oldboy", "南京") # s3 = School("oldboy", "东京") # # msg = """ # 1、老男孩 北京校区 # 2、老男孩 南京校区 # 3、老男孩 东京校区 # # """ # while True: # print(msg) # menu ={ # '1': s1, # '2': s2, # '3':s3 # } # choice = input('选择学校') # school_obj = menu[choice] # name = input('课程名>>: ') # price = input('课程费用>>: ') # period = input('课程周期>>: ') # new_course = Course(name, price, period, school_obj) # print('课程 【%s】 属于 【%s】学校' %(new_course.name, new_course.school.name)) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # # # 1、老男孩 北京校区 # # 2、老男孩 南京校区 # # 3、老男孩 东京校区 # # # # # # 选择学校1 # # 课程名>>: py # # 课程费用>>: 999 # # # # 课程周期>>: 课程 【py】 属于 【oldboy】学校 # # # # 1、老男孩 北京校区 # # 2、老男孩 南京校区 # # 3、老男孩 东京校区 # # # # 选择学校 ''' # ------------------------------------------------------------ # # 10、组合应用,给区域学校添加课程2 # # 这个通过的是列表来实现,相对于上面那种,差了一些 # ------------------------------------------------------------ ''' # # class School: # def __init__(self, name ,addr): # self.name = name # self.addr = addr # self.course_list = [] # def zhao_sheng(self): # print("%s 正在招生" %self.name) # # class Course: # def __init__(self, name, price, period): # self.name = name # self.price = price # self.period = period # # s1 = School("oldboy", "北京") # s2 = School("oldboy", "南京") # s3 = School("oldboy", "东京") # # c1 = Course('linux', 11, '1h') # c2 = Course('python', 10, '66h') # # s1.course_list.append(c1) # s2.course_list.append(c2) # print(s1.__dict__) # # for obj in s1.course_list: # print(obj.name, obj.price) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # {'name': 'oldboy', 'addr': '北京', 'course_list': [<__main__.Course object at 0x00000000039DA390>]} # # linux 11 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 10、组合应用,给区域学校添加课程2 # # 这个通过的是列表来实现,相对于上面那种,差了一些 # ------------------------------------------------------------ ''' # # class School: # def __init__(self, name ,addr): # self.name = name # self.addr = addr # self.course_list = [] # def zhao_sheng(self): # print("%s 正在招生" %self.name) # # class Course: # def __init__(self, name, price, period): # self.name = name # self.price = price # self.period = period # # s1 = School("oldboy", "北京") # s2 = School("oldboy", "南京") # s3 = School("oldboy", "东京") # # c1 = Course('linux', 11, '1h') # c2 = Course('python', 10, '66h') # # s1.course_list.append(c1) # s2.course_list.append(c2) # print(s1.__dict__) # # for obj in s1.course_list: # print(obj.name, obj.price) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # {'name': 'oldboy', 'addr': '北京', 'course_list': [<__main__.Course object at 0x00000000039DA390>]} # # linux 11 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 10、组合应用,给区域学校添加课程2 # # 这个通过的是列表来实现,相对于上面那种,差了一些 # ------------------------------------------------------------ ''' # # class School: # def __init__(self, name ,addr): # self.name = name # self.addr = addr # self.course_list = [] # def zhao_sheng(self): # print("%s 正在招生" %self.name) # # class Course: # def __init__(self, name, price, period): # self.name = name # self.price = price # self.period = period # # s1 = School("oldboy", "北京") # s2 = School("oldboy", "南京") # s3 = School("oldboy", "东京") # # c1 = Course('linux', 11, '1h') # c2 = Course('python', 10, '66h') # # s1.course_list.append(c1) # s2.course_list.append(c2) # print(s1.__dict__) # # for obj in s1.course_list: # print(obj.name, obj.price) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # {'name': 'oldboy', 'addr': '北京', 'course_list': [<__main__.Course object at 0x00000000039DA390>]} # # linux 11 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 11、类的继承--数值属性继承 # # # # # # 类的继承跟现实生活中的父、子、孙子、重孙子、继承关系一样,父类又称为基类 # ------------------------------------------------------------ ''' # # class Dad: # '这个是一个爸爸类' # money = 10 # def __init__(self, name): # print("爸爸") # self.name = name # def son(self): # print("爸爸类的son ", self.name ) # # class Son(Dad): # pass # # # money = 99999 # # def __init__(self, name, age): # # self.name = name # # self.age = age # # def son(self): # # print("儿子类的son",self.name) # # # print(" Son.money: ", Son.money) # print(" Dad.__dict__: ", Dad.__dict__) # print("Son.__dict__: ", Son.__dict__) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # Son.money: 10 # # Dad.__dict__: {'__module__': '__main__', '__doc__': '这个是一个爸爸类', 'money': 10, '__init__': <function Dad.__init__ at 0x0000000002274620>, 'son': <function Dad.son at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>} # # Son.__dict__: {'__module__': '__main__', '__doc__': None} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 12、类数值属性继承1 # # # 以下列例子为基础,数值属性查找:如果son中有数值属性,那么,就会使用自己的,如果没有, # # # 就回去Dad类中找 # # # 如果是Dad类中有添加数值属性,Son是继承了Dad类的,如果本身没有的话,仍然能够去Dad类中查找 # # # 本质上,两者的数值属性是相互独立 # ------------------------------------------------------------ ''' # # class Dad: # '这个是一个爸爸类' # money = 10 # def __init__(self, name): # print("爸爸") # self.name = name # def son(self): # print("爸爸类的son ", self.name ) # # class Son(Dad): # # pass # money = 99999 # # # print(" Dad.__dict__: ", Dad.__dict__) # print("Son.__dict__: ", Son.__dict__) # print(" Dad.money: ", Dad.money) # print(" Son.money: ", Son.money) # print("分割线".center(100,"-")) # # Son.money = 66 # Dad.car = "法拉利" # print(" Dad.money: ", Dad.money) # print(" Son.money: ", Son.money) # print(" Son.car: ", Son.car) # # # print(" Dad.__dict__: ", Dad.__dict__) # print("Son.__dict__: ", Son.__dict__) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # Dad.__dict__: {'__module__': '__main__', '__doc__': '这个是一个爸爸类', 'money': 10, '__init__': <function Dad.__init__ at 0x00000000029A4620>, 'son': <function Dad.son at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>} # # Son.__dict__: {'__module__': '__main__', 'money': 99999, '__doc__': None} # # Dad.money: 10 # # Son.money: 99999 # # ------------------------------------------------分割线------------------------------------------------- # # Dad.money: 10 # # Son.money: 66 # # Son.car: 法拉利 # # Dad.__dict__: {'__module__': '__main__', '__doc__': '这个是一个爸爸类', 'money': 10, '__init__': <function Dad.__init__ at 0x00000000029A4620>, 'son': <function Dad.son at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>, 'car': '法拉利'} # # Son.__dict__: {'__module__': '__main__', 'money': 66, '__doc__': None} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 13、类函数属性继承 # # # # ------------------------------------------------------------ ''' # # class Dad: # '这个是一个爸爸类' # money = 10 # def __init__(self, name): # print("爸爸") # self.name = name # def son(self): # print("爸爸类的son ", self.name ) # # class Son(Dad): # # pass # money = 99999 # # # print(" Dad.__dict__: ", Dad.__dict__) # print("Son.__dict__: ", Son.__dict__) # s = Son("zhang") # print(s.name, s.money) # s.son() # print("分割线".center(100,"-")) # # print(" Dad.__dict__: ", Dad.__dict__) # print("Son.__dict__: ", Son.__dict__) # # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # Dad.__dict__: {'__module__': '__main__', '__doc__': '这个是一个爸爸类', 'money': 10, '__init__': <function Dad.__init__ at 0x00000000029A4620>, 'son': <function Dad.son at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>} # # Son.__dict__: {'__module__': '__main__', 'money': 99999, '__doc__': None} # # 爸爸 # # zhang 99999 # # 爸爸类的son zhang # # ------------------------------------------------分割线------------------------------------------------- # # Dad.__dict__: {'__module__': '__main__', '__doc__': '这个是一个爸爸类', 'money': 10, '__init__': <function Dad.__init__ at 0x00000000029A4620>, 'son': <function Dad.son at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>} # # Son.__dict__: {'__module__': '__main__', 'money': 99999, '__doc__': None} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 14、类函数属性继承2 # # # Dad类与继承类Son中,如果Son中有自己的函数属性就用自己的 # ------------------------------------------------------------ ''' # # class Dad: # '这个是一个爸爸类' # money = 10 # def __init__(self, name): # print("这个是一个Dad类中的__init__") # self.name = name # def son(self): # print("爸爸类的son ", self.name ) # # class Son(Dad): # money = 99999 # def son(self): # print("Son类的son函数 ", self.name ) # # # print(" Dad.__dict__: ", Dad.__dict__) # print("Son.__dict__: ", Son.__dict__) # s = Son("zhang") # d = Dad("张一鸣") # # print("分割线Son".center(100,"-")) # print(s.name, s.money) # s.son() # # print("分割线 Dad".center(100,"-")) # print(d.name, d.money) # d.son() # print("分割线".center(100,"-")) # # print(" Dad.__dict__: ", Dad.__dict__) # print("Son.__dict__: ", Son.__dict__) # # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # Dad.__dict__: {'__module__': '__main__', '__doc__': '这个是一个爸爸类', 'money': 10, '__init__': <function Dad.__init__ at 0x00000000029B4620>, 'son': <function Dad.son at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>} # # Son.__dict__: {'__module__': '__main__', 'money': 99999, 'son': <function Son.son at 0x00000000039EE840>, '__doc__': None} # # 这个是一个Dad类中的__init__ # # 这个是一个Dad类中的__init__ # # -----------------------------------------------分割线Son----------------------------------------------- # # zhang 99999 # # Son类的son函数 zhang # # ----------------------------------------------分割线 Dad----------------------------------------------- # # 张一鸣 10 # # 爸爸类的son 张一鸣 # # ------------------------------------------------分割线------------------------------------------------- # # Dad.__dict__: {'__module__': '__main__', '__doc__': '这个是一个爸爸类', 'money': 10, '__init__': <function Dad.__init__ at 0x00000000029B4620>, 'son': <function Dad.son at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>} # # Son.__dict__: {'__module__': '__main__', 'money': 99999, 'son': <function Son.son at 0x00000000039EE840>, '__doc__': None} # # # # Process finished with exit code 0 # 08 接口继承 # 08 接口继承 ''' # ------------------------------------------------------------ # # 15、接口继承引入 # # # 所有的类都有一个共同的属性方式,这个时候,可以将他们提取出来,放到同一个类当中,作为一 # # # 个基类 # # # 这个属于:继承基类的方法,并且做出自己的改变或者扩展(代码重用) # # # 但是意义并不是很大,甚至常常是有害的,因为它使得子类与基类出现强耦合; # # # 因此少用 # ------------------------------------------------------------ ''' # # class All_file: # def read(self): # print('All_file read') # # def write(self): # print('All_file write') # # class Disk(All_file): # # def read(self): # # print('disk read') # # # # def write(self): # # print('disk write') # pass # # class Cdroom: # # def read(self): # # print('Cdroom read') # # # # def write(self): # # print('Cdroom write') # pass # # class Mem(All_file): # # def read(self): # # print('Mem read') # pass # # # m1 = Mem() # m1.read() # m1.write() # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # All_file read # # All_file write # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 15、 @abc.abstractmethod, 接口继承 # # # 以下面为例子,所有类中的共同有的函数属性--write、read全部提取出来到,All_file类中 # # # 通过@abc.abstractmethod进行修饰,所有的继承的类必须要有这个函数 # ------------------------------------------------------------ ''' # # import abc # class All_file: # @abc.abstractmethod # def read(self): # pass # # @abc.abstractmethod # def write(self): # pass # # class Disk(All_file): # def read(self): # print('disk read') # # def write(self): # print('disk write') # # pass # # class Cdroom(All_file): # def read(self): # print('Cdroom read') # # def write(self): # print('Cdroom write') # # class Mem(All_file): # def read(self): # print('Mem read') # # def write(self): # print('Mem write') # # # m1 = Mem() # m1.read() # m1.write() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # Mem read # # Mem write # # # # Process finished with exit code 0 # ''' # ------------------------------------------------------------ # # 15.1、 接口继承中,继承者缺少被继承者要求的函数 # # # 继承者==son类,被继承者==parent类,如果继承过程中,parent类接口要求2个函数属性, # # # son中有1个,会报错 # # # 下面例子, # # # 被继承者 == All_file; 继承者 == Mem Directory # ------------------------------------------------------------ ''' # # import abc # class All_file: # @abc.abstractmethod # def read(self): # pass # # @abc.abstractmethod # def write(self): # pass # # # Directory,调用内部函数,会报错,因为Directory中没有write函属性 # class Directory(All_file): # def read(self): # print('Directory read') # # # def write(self): # # print('Directory write') # # pass # # # class Mem(All_file): # def read(self): # print('Mem read') # # # def write(self): # # print('Mem write') # # # m1 = Mem() # 实例化,不调用,不会报错 # # d1 = Directory # 仅仅只是实例化,不会报错 # d1.read() # 报错!!实例化后,调用内部函数,因为Directory中没有write函属性 # d1.write() # 报错!!实例化后,调用内部函数,因为Directory中没有write函属性 # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # Traceback (most recent call last): # # File "D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py", line 1192, in <module> # # d1.read() # 报错!!实例化后,调用内部函数,因为Directory中没有write函属性 # # TypeError: read() missing 1 required positional argument: 'self' # # # # Process finished with exit code 1 ''' # ------------------------------------------------------------ # # 15.2、 接口继承中,继承者有被继承者要求的函数之外,还有其他函数属性 # # # 以下面为例子,所有类中的共同有的函数属性--write、read全部提取出来到,All_file类中 # # # 通过@abc.abstractmethod进行修饰,所有的继承的类必须要有这个函数, # # # 但是,继承者在这个基础上可以有其他的函数 # ------------------------------------------------------------ ''' # # import abc # class All_file: # @abc.abstractmethod # def read(self): # pass # # @abc.abstractmethod # def write(self): # pass # # class Disk(All_file): # def read(self): # print('disk read') # # def write(self): # print('disk write') # # # class Mem(All_file): # def read(self): # print('Mem read') # # def read_write(self): # print('Mem read_write') # # def write(self): # print('Mem write') # # # # # m1 = Mem() # m1.read() # m1.read_write() # m1.write() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # Mem read # # Mem read_write # # Mem write # # # # Process finished with exit code 0 # 09 继承顺序之mro线性顺序列表 # 09 继承顺序之mro线性顺序列表 ''' # ------------------------------------------------------------ # # 16、继承顺序引入( 新式类,广度优先) # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # # class B(A): # def test(self): # print("B") # # class C(A): # def test(self): # print("C") # # class D(B): # def test(self): # print("D") # # class E(C): # def test(self): # print("E") # # # # class F(D,E): # def test(self): # print("F") # # # f1 = F() # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # F # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 16.1、继承顺序引入 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # def test(self): # print("B") # pass # # class C(A): # def test(self): # print("C") # pass # # class D(B): # def test(self): # print("D") # pass # # class E(C): # def test(self): # print("E") # # pass # # class F(D,E): # pass # # # f1 = F() # F --> D # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # D # # # # Process finished with exit code 0 # ''' # ------------------------------------------------------------ # # 16.2、继承顺序引入 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # def test(self): # print("B") # pass # # class C(A): # def test(self): # print("C") # pass # # class D(B): # pass # # class E(C): # def test(self): # print("E") # # pass # # class F(D,E): # # pass # # # f1 = F() # F --> D --> B # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # B # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 16.3、继承顺序引入 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # pass # # class C(A): # def test(self): # print("C") # pass # # class D(B): # pass # # class E(C): # def test(self): # print("E") # pass # # class F(D,E): # pass # # # F --> D --> B --> 找不到(注意,没有找A),结束; # # 回来第二条分支 F --> E # f1 = F() # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # E # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 16.4、继承顺序引入 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # pass # # class C(A): # def test(self): # print("C") # pass # # class D(B): # pass # # class E(C): # pass # # class F(D,E): # pass # # # F --> D --> B --> 找不到(注意,没有找A),结束; # # 回来第二条分支 F --> E --> C # f1 = F() # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # C # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 16.5、继承顺序引入 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # pass # # class C(A): # pass # # class D(B): # pass # # class E(C): # pass # # class F(D,E): # pass # # # F --> D --> B --> 找不到(注意,没有找A),结束; # # 回来第二条分支 F --> E --> C --> A(这个是最终的类,如果在没有就会报错) # f1 = F() # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # A # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 16.6、总结:继承顺序引入 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # # # 这种方式称之为:广度优先!python3中的所有的类都是新式类,python2x中的是经典类 # # # 新式类:满足class A(object),即父类是object类_main__.A'>, <class 'object'>(见序号17中的print(F.__mro__) ), # # # python3中都是新式类,因此 class A == class A(object) # # # 经典类:???? # # # 当类是经典类时,多继承情况下,会按照深度优先方式查找 # # # # # # Python成长之路【第九篇】:Python基础之面向对象 - Mr_Albert - 博客园 # # # https://www.cnblogs.com/albert0924/p/8921709.html # ------------------------------------------------------------ ''' ''' # ------------------------------------------------------------ # # 17、继承顺序的的确定 # # # Python到底是如何实现继承的,对于你定义的每一个类,Python会计算出一个方法 # # # 解析顺序(MRO)元组,这个MRO元组就是一个简单的所有基类的线性顺序列表 # # # # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # pass # # class C(A): # pass # # class D(B): # pass # # class E(C): # pass # # class F(D,E): # pass # # # F --> D --> B --> 找不到(注意,没有找A),结束; # # 回来第二条分支 F --> E --> C --> A(这个是最终的类,如果在没有就会报错) # f1 = F() # f1.test() # print(F.__mro__) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # A # # (<class '__main__.F'>, <class '__main__.D'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>) # # # # Process finished with exit code 0 # 10 在python2中的继承顺序是什么 # 10 在python2中的继承顺序是什么 ''' # ------------------------------------------------------------ # # 18、继承顺序引入( 经典类,深度优先) # # # 经典类: class A; 新式类:class A(object) # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # # # # # # 编译环境python2.7( 没有标注的都是python3)( 没有标注的都是python3) # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # # class B(A): # def test(self): # print("B") # # class C(A): # def test(self): # print("C") # # class D(B): # def test(self): # print("D") # # class E(C): # def test(self): # print("E") # # # # class F(D,E): # def test(self): # print("F") # # # f1 = F() # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # F # # # # Process finished with exit code 0 # ''' # ------------------------------------------------------------ # # 18.1、继承顺序引入 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # # # # # # 编译环境python2.7( 没有标注的都是python3) # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # def test(self): # print("B") # pass # # class C(A): # def test(self): # print("C") # pass # # class D(B): # def test(self): # print("D") # pass # # class E(C): # def test(self): # print("E") # # pass # # class F(D,E): # pass # # # f1 = F() # F --> D # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # D # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 18.2、继承顺序引入 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # # # # # # 编译环境python2.7( 没有标注的都是python3) # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # def test(self): # print("B") # pass # # class C(A): # def test(self): # print("C") # pass # # class D(B): # pass # # class E(C): # def test(self): # print("E") # # pass # # class F(D,E): # # pass # # # f1 = F() # F --> D --> B # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # B # # # # Process finished with exit code 0 # # ''' # ------------------------------------------------------------ # # 18.3、继承顺序引入(与16.3对比) # # # 深度优先,会使用分支一找到A # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # # # # # # 编译环境python2.7( 没有标注的都是python3) # ------------------------------------------------------------ ''' # class A: # def test(self): # print("A") # pass # # class B(A): # pass # # class C(A): # def test(self): # print("C") # pass # # class D(B): # pass # # class E(C): # def test(self): # print("E") # pass # # class F(D,E): # pass # # # F --> D --> B --> A结束; # # 深度优先 # f1 = F() # f1.test() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # A # # # # Process finished with exit code 0 # # # # # ''' # ------------------------------------------------------------ # # 18.4、继承顺序引入(与16.3对比) # # # class A: 经典类,深度优先; class A(object): 新式类,广度优先 # # # 广度优先,B找不到,回来通过E找,后面的同python3的情况 # # # 继承结构:分支一: F --> D --> B --> A; # # # 分支二: F --> E --> C --> A; # # # # # # 编译环境python2.7 # ------------------------------------------------------------ ''' # class A(object): # def test(self): # print("A") # pass # # class B(A): # pass # # class C(A): # def test(self): # print("C") # pass # # class D(B): # pass # # class E(C): # def test(self): # print("E") # pass # # class F(D,E): # pass # # # # F --> D --> B --> 找不到(注意,没有找A),结束; # # # 回来第二条分支 F --> E # # 深度优先 # f1 = F() # f1.test() # # D:C_cachepyday18_WenJianChuLivenvScriptspython.exe D:/C_cache/py/python2x_env/cache.py # E # # Process finished with exit code 0 # # 11 在子类中调用父类方法part1 # 11 在子类中调用父类方法part1 ''' # ------------------------------------------------------------ # # 19、子类与父类中__init__有重复 # ------------------------------------------------------------ ''' # # class Vehicle: # Country = 'China' # # def __init__(self, name, speed, load, power): # self.name = name # self.speed = speed # self.load = load # self.power = power # # def run(self): # print("Vehicle父类run启动!") # print("开动了……") # print("开动了……") # print("开动了……") # print("Vehicle父类run结束!") # # class Subway(Vehicle): # def __init__(self, name, speed, load, power, line): # self.name = name # self.speed = speed # self.load = load # self.power = power # self.line = line # # def show_info(self): # print(self.name, self.speed, self.load, self.power, self.line) # # def run(self): # print(" %s %s 线,开动啦" % (self.name, self.line)) # # # line13 = Subway('北京地铁', '10k/s', 66666, '电', 13) # line13.show_info() # line13.run() # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # 北京地铁 10k/s 66666 电 13 # # 北京地铁 13 线,开动啦 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 20、关于self自动传入参数的情况:1、实例化; 2、对象调用类方法 # ------------------------------------------------------------ # ''' ''' # ------------------------------------------------------------ # # 21、子类与父类中__init__重复解决方式 # ------------------------------------------------------------ # ''' # class Vehicle: # Country = 'China' # def __init__(self, name, speed, load, power): # self.name = name # self.speed = speed # self.load = load # self.power = power # def run(self): # print("Vehicle父类run启动!") # print("开动了……") # print("开动了……") # print("开动了……") # print("Vehicle父类run结束!") # # class Subway(Vehicle): # # 继承了父类中的__init__,同时自己派生了自己的数值属性,self.line = line # def __init__(self, name, speed, load, power, line): # # 子类调用父类方法 # Vehicle.__init__(self, name, speed, load, power) # # 子类派生自己的属性 # self.line = line # # def show_info(self): # print(self.name, self.speed, self.load, self.power, self.line) # # def run(self): # print(" %s %s 线,开动啦" %(self.name, self.line)) # # # line13 = Subway('北京地铁', '10k/s',66666, '电', 13) # line13.show_info() # line13.run() # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # 北京地铁 10k/s 66666 电 13 # # 北京地铁 13 线,开动啦 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 22、子类调用父类的方法 # ------------------------------------------------------------ ''' # # class Vehicle: # Country = 'China' # def __init__(self, name, speed, load, power): # self.name = name # self.speed = speed # self.load = load # self.power = power # def run(self): # print("Vehicle父类run启动!") # print("开动了……") # print("开动了……") # print("开动了……") # print("Vehicle父类run结束!") # # class Subway(Vehicle): # def __init__(self, name, speed, load, power, line): # # 子类调用父类方法 # Vehicle.__init__(self, name, speed, load, power) # self.line = line # # def show_info(self): # print(self.name, self.speed, self.load, self.power, self.line) # # def run(self): # # 子类调用父类方法 # Vehicle.run(self) # 注意,这里要加入一个self的参数 # print(" %s %s 线,开动啦" %(self.name, self.line)) # # # line13 = Subway('北京地铁', '10k/s',66666, '电', 13) # line13.show_info() # line13.run() # # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # 北京地铁 10k/s 66666 电 13 # # Vehicle父类run启动! # # 开动了…… # # 开动了…… # # 开动了…… # # Vehicle父类run结束! # # 北京地铁 13 线,开动啦 # # # # Process finished with exit code 0 # ------------------------------------------------分割线------------------------------------------------- # 12 super调用父类的方法 # 12 super调用父类的方法 ''' # ------------------------------------------------------------ # # 23、super调用父类的方法 # # # =====>>>>>>上面的那种子类调用父类的方法还不够好 下面的1、2、3的表达方式是等价的 1、super().__init__(name, speed, load, power) 2、super(__class__,self).__init__(name,speed,load,power) # __class__是类名,见# print(line13.__class__) 3、super(Subway,self).__init__(name,speed,load,power) # ------------------------------------------------------------ ''' # # class Vehicle: # Country = 'China' # def __init__(self, name, speed, load, power): # self.name = name # self.speed = speed # self.load = load # self.power = power # def run(self): # print("Vehicle父类run启动!") # print("开动了……") # print("开动了……") # print("开动了……") # print("Vehicle父类run结束!") # # class Subway(Vehicle): # def __init__(self, name, speed, load, power, line): # # 子类调用父类方法 # super().__init__(name, speed, load, power) # self.line = line # # def run(self): # # 子类调用父类方法 # super().run() # 注意这里,没有self # print(" %s %s 线,开动啦" %(self.name, self.line)) # # def show_info(self): # print(self.name, self.speed, self.load, self.power, self.line) # # line13 = Subway('北京地铁', '10k/s',66666, '电', 13) # line13.show_info() # line13.run() # print(line13.__class__) # # # D:Anaconda3python.exe D:/C_cache/py/day25_JingTai_ZuHe_JiCheng/day25_JingTai_ZuHe_JiCheng.py # # 北京地铁 10k/s 66666 电 13 # # Vehicle父类run启动! # # 开动了…… # # 开动了…… # # 开动了…… # # Vehicle父类run结束! # # 北京地铁 13 线,开动啦 # # <class '__main__.Subway'> # # # # Process finished with exit code 0