海洋馆网站建设移动网站开发入门

当前位置: 首页 > news >正文

海洋馆网站建设,移动网站开发入门,松溪网站建设,湖南网站建设公司磐石网络练习44 继承和组合 永远记住这一点#xff1a;继承的大多数用法都可以用组合#xff08;composition#xff09;来简化或替换。并且无论如何都要避免多重继承。 内容提要#xff1a;

  1. 什么是继承#xff1f; #xff08;1#xff09;隐式继承 #xff08;2#x…练习44 继承和组合 永远记住这一点继承的大多数用法都可以用组合composition来简化或替换。并且无论如何都要避免多重继承。 内容提要
  2. 什么是继承 1隐式继承 2显示继承 3改变前后 4三种组合 5为何用super() 6组合 2. 何时使用继承和组合 什么是继承 继承用来表明一个类将从其父类那里获得大多数或所有特性。当你在做这种专门化时有三种父类和子类可以交互的方法 1对子类的行为意味着对父类的行为。——隐式继承 2子类上的操作会覆盖父类上的操作。——显示继承 3子类上的操作会更改父类上的操作。 1、隐式继承 代码ex44a.py class Parent(object): #创建父类def implicit(self): #创建函数print(PARENT implicit())class Child(Parent): #创建子类Child继承至父类Parentpassdad Parent() #Parent()类实例化 son Child() #Child()类实例化dad.implicit() #调用父类内部函数 son.implicit() #调用函数继承至父类输出结果 PARENT implicit() PARENT implicit()说明 pass关键词“暂时跳过”pass 是一种空操作解释器执行到它的时候除了检查语法是否合法什么也不做就直接跳过。使用在函数、类、循环体或者条件判断语句中。用于补充语法的完整性要不然代码会报错。 Child()的类底下使用pass空块没有定义实际内容但它仍旧可以可以调用函数implicit()就是因为它继承了父类Parent()的函数这就是隐式继承。因此如果将函数放在父类中(比如 Parent然后所有子类比如 Child会自动获得这些特性。对于需要写很多重复代码的类来说非常方便。 2、显式继承 代码ex44b.py #显式继承——子类中定义一个与父类同名的函数以替换父类中的函数功能 class Parent(): #创建父类def override(self): #创建父类的函数print(Parent override())class Child(Parent): #创建子类Child继承至父类Parentdef override(self): #创建与父类中函数同名的函数但功能与其不同print(Child override())dad Parent() #实例化父类 son Child() #实例化子类dad.override() #调用父类中的函数override() son.override() #调用子类中的函数override()输出结果 Parent override() Child override()说明 隐式调用函数的问题在于有时希望子类与父类的行为有所不同。只需要在子类 Child 中定义一个与父类Parent同名函数子类上的操作会覆盖父类上的操作。如上代码中父类与子类都同时存在的override()函数运行之后的结果不同。 3、修改前后 代码ex44c.py #子类上的操作会更改父类上的操作 #修改类中函数前后 class Parent(): #创建父类def altered(self): #创建父类函数print(Parent altered())class Child(Parent): #创建子类def altered(self): #创建与父类函数同名的子类函数覆盖父类函数print(Child, Before Parent altered()) #打印super().altered() #利用super(超类)调用父类中的函数print(Child, After Parent altered()) #继续打印dad Parent() #实例化父类 son Child() #实例化子类dad.altered() #调用父类函数 son.altered() #调用子类函数输出结果 Parent altered() Child, Before Parent altered() Parent altered() Child, After Parent altered()说明 第三种使用继承的方式是覆盖的一种特殊情况可以在父类版本运行前后更改子类的函数内容。首先与之前一样在子类中创建一个与父类同名的函数覆盖原先的函数其次使用super()函数调用父类中的函数。 4、以上三中继承形式结合 过一遍这段代码的每一行并每一行加上注释说明它的作用。 #三者结合 class Parent(): #创建父类def override(self): #创建父类函数overrideprint(Parent override()) #函数的行为打印字符串def implicit(self): #创建父类函数implicit()print(Parent implicit()) #函数的行为打印字符串def altered(self): #创建父类函数altered()print(Parent altered()) #函数的行为打印字符串class Child(Parent): #创建子类def override(self): #创建与父类中override同名的函数覆盖父类中该函数的内容print(Child override())def altered(self): #创建与父类中altered同名函数覆盖父类该函数内容print(Child, Before Parent altered())super().altered() #调用函数super(超类)再次继承父类中的函数altered()print(Child, After Parent altered())dad Parent() #父类实例化 son Child() #子类实例化dad.implicit() #调用父类函数implicit() son.implicit() #子类中未定义函数implicit(),但隐式继承了父类也可调用该函数且内容相同dad.override() #调用父类override()函数 son.override() #调用子类override()函数dad.altered() #调用父类altered()函数 son.altered() #调用子类altered()函数输出结果 Parent implicit() Parent implicit() Parent override() Child override() Parent altered() Child, Before Parent altered() Parent altered() Child, After Parent altered()5、用super()的理由 什么是多重继承之前练习有学习了解 如下代码 class SuperFun(Child, BadStuff): pass如上代码中创建的类SuperFun()同时继承自Child()和BadStuff()两个类即一个类继承了一个或多个类就是“多重继承”。 在出现多重继承的情况下对任何 SuperFun 的实例执行隐式操作时Python 都必须在 Child 类和 BadStuff 类的层级结构中查找可能的函数不过它需要以一致的顺序来执行这项操作。为了做到这一点Python 使用了“方法解析顺序”(method resolution orderMRO)和一种被称为 C3 的算法这种算法很复杂因此使用super()函数可以帮助我们在需要修改的地方进行处理即可。 用init来使用super() super() 最常用的用法其实是在子类中使用基类的 init 函数。这通常是你在一个子类中唯一需要做一些操作然后在父类中完成初始化的地方。 示例如下 class Child(Parent):def init(self, stuff): #初始化函数self.stuff stuffsuper(Child,self).init() #调用父类的初始化函数6、组合 继承很有用但是还有一种能实现相同效果的方法就是使用其他类和模块而不是依赖于隐式继承。当使用继承时子类中存在大量的编写新代码来替换或者更改函数功能的时候可以通过调用模块中的函数来实现。示例如下 代码ex44e.py class Other(): #创建一个额外的类Other()def override(self): #创建函数print(Other override())def implicit(self): #创建类的函数print(Other implicit())def altered(self): #创建类的函数print(Other altered())class Child(): #创建类Child()# def init(self): #创建初始化函数#创建Other()类与Child()之间的桥梁,也可以没有但是调用Other()类中的函数方式会有所不用# self.other Other() def implicit(self): #创建函数implicit()# self.other.implicit() #对应第一种调用Other()类中的implicit()函数Other().implicit() #对应第二种调用Other()类中的implicit()函数def override(self): #创建函数override()print(Child override())def altered(self): #创建altered()函数print(Child, Before Other altered())# self.other.altered() #第一种调用Other()类中的altered()函数Other().altered() #第二种调用Other()类中的函数altered()print(Child, After Other altered())son Child() #实例化Child类son.implicit() #调用函数implicit() son.override() #调用函数override() son.altered() #调用函数altered()输出结果 Other implicit() Child override() Child, Before Other altered() Other altered() Child, After Other altered()说明 可以看到Child 和 Other 中的大多数代码都是相同的可以完成相同的事情。唯一的区别是必须定义一个 Child.implicit 函数来完成这个动作。 其中在Child()类中调用Other()类的方法有两种。 第一种在Child类的init函数中定义other属性self.other Other(),后续可直接使用self.other.函数名来调用Other类中的函数。 第二种直接在Child类中实例化Other类后调用例如Other().implicit() 问题是否可以将Other类放入Other.py的模块中去调用它 可以代码如下 文件other.py class Other(): #创建一个额外的类Other()def override(self): #创建函数print(Other override())def implicit(self): #创建类的函数print(Other implicit())def altered(self): #创建类的函数print(Other altered())文件ex44_Child.py from other import Other #从模块Other中调用Other类class Child(): #创建类Child()def init(self): #创建初始化函数#创建Other()类与Child()之间的桥梁,也可以没有但是调用Other()类中的函数方式会有所不用self.other Other() def implicit(self): #创建函数implicit()self.other.implicit() #对应第一种调用Other()类中的implicit()函数# Other().implicit() #对应第二种调用Other()类中的implicit()函数def override(self): #创建函数override()print(Child override())def altered(self): #创建altered()函数print(Child, Before Other altered())self.other.altered() #第一种调用Other()类中的altered()函数# Other().altered() #第二种调用Other()类中的函数altered()print(Child, After Other altered())son Child() #实例化Child类son.implicit() #调用函数implicit() son.override() #调用函数override() son.altered() #调用函数altered()输出结果 Other implicit() Child override() Child, Before Other altered() Other altered() Child, After Other altered()说明 可以通过other.py文件调用Other类只需要在ex44_Child.py 文件的脚本代码中加入import调用模块即from other import Other。在Child类中的调用方式与之前完全相同。 7、何时使用继承和组合 “继承与组合”的问题可以归结为试图解决可复用代码的问题。 ——继承通过在父类中创建隐含相同功能的函数的机制来解决这个问题。 ——组合通过提供模块以及调用其他类中的函数来解决这个问题。 三个指导方针建议但可能无法遵守 1、无论如何都要避免多重继承因为它太复杂而且不可靠。 2、使用组合将代码打包到模块中这些模块可以用于许多不同的、不相关的地方和情境。 3、代码的可复用部分之间有清楚的关联很明显的父类与子类关系如动物和猫或者必须要用继承时用继承。 附加练习 阅读 : http://www.python.org/dev/peps/pep-0008/ 文件该网站需要翻墙。