类的继承

基本概念

定义

格式如下

继承中的访问控制

class Animal:
__CNOUT = 0
HEIGHT = 0 def __init__(self,age,weight,height):
self.__CNOUT =self.__CNOUT + 1
self.age = age
self.__weight = weight
self.HEIGHT = height def eat(self):
print('{} eat'.format(self.__class__.__name__)) def __getweight(self):
print(self.__weight) @classmethod
def showcount1(cls):
print(cls.__CNOUT) @classmethod
def __showcount2(cls):
print(cls.__CNOUT) class Cat(Animal):
NAME = 'CAT' c = Cat(3,5,15)
c.eat()
c.showcount1()#注意此处的cls.__COUNT仍为0 print(c.NAME)
print(c.__dict__)#{'_Animal__CNOUT': 1, 'age': 3, '_Animal__weight': 5, 'HEIGHT': 15}
print(Cat.__dict__)#{'__module__': '__main__', 'NAME': 'CAT', '__doc__': None}
print(Animal.__dict__)#{'__module__': '__main__', '_Animal__CNOUT': 0, 'HEIGHT': 0,...}

方法的重写、覆盖override

class Animal:
def shout(self):
print('Animal shout') class Cat(Animal):
#覆盖父类的方法
def shout(self):
print('miao')
#覆盖自身的方法,显示调用了父类的方法
def shout(self):
print(super(Cat, self))
super().shout() a = Animal()
a.shout()
c =Cat()
c.shout() #输出结果:
# Animal shout
# <super: <class 'Cat'>, <Cat object>>
# Animal shout

class Animal:
@classmethod
def class_method(cls):
print('class_method_animal') @staticmethod
def static_method():
print('static_method_animal') class Cat(Animal):
@classmethod
def class_method(cls):
print('class_method_cat') @staticmethod
def static_method():
print('static_method_cat') c = Cat()
c.class_method()
c.static_method()
#输出结果:
# class_method_cat
# static_method_cat

继承中的初始化

示例1

class A:
def __init__(self):
self.a1 = 'a1'
self.__a2 = 's2'
print('A init') class B(A):
pass
b = B()
print(b.__dict__) #{'a1': 'a1', '_A__a2': 's2'}

示例2

class A:
def __init__(self):
self.a1 = 'a1'
self.__a2 = 's2'
print('A init') class B(A):
def __init__(self):
self.b1 = 'b1'
print('B init')
b = B()
print(b.__dict__) #{'b1': 'b1'}

class A:
def __init__(self):
self.a1 = 'a1'
self.__a2 = 's2'
print('A init') class B(A):
def __init__(self):
# A.__init__(self)
# super(B,self).__init__()
super().__init__()
self.b1 = 'b1'
print('B init')
b = B()
print(b.__dict__) #{'a1': 'a1', '_A__a2': 's2', 'b1': 'b1'}

如何正确初始化

 

class Animal:
def __init__(self,age):
print('Animal init')
self.age =age def show(self):
print(self.age) class Cat(Animal):
def __init__(self,age,weight):
super().__init__(age)#c.show()结果为11
print('Cat init')
self.age = age + 1
self.weight = weight
# super().__init__(age)#c.show()结果为10
c = Cat(10,5)
c.show()
# c.__dict__ {'age': 11, 'weight': 5}

class Animal:
def __init__(self,age):
print('Animal init')
self.__age =age def show(self):
print(self.__age) class Cat(Animal):
def __init__(self,age,weight):
super().__init__(age)
print('Cat init')
self.__age = age + 1
self.__weight = weight
c = Cat(10,5)
c.show()
print(c.__dict__)#{'_Animal__age': 10, '_Cat__age': 11, '_Cat__weight': 5}

Python不同版本的类

多继承

多继承弊端

Python多继承实现

class ClassName(基类列表):
类体

多继承的缺点

Mixin

class Printable:
def _print(self):
print(self.content) class Document:#假设为第三方库,不允许修改
def __init__(self,content):
self.content = content class Word(Document):pass#假设为第三方库,不允许修改
class Pdf(Document):pass#假设为第三方库,不允许修改 class PrintableWord(Printable,Word):pass
print(PrintableWord.__dict__)
print(PrintableWord.mro())
pw = PrintableWord('test string')
pw._print()

def printable(cls):
def _print(self):
print(self.content,'装饰器')
cls.print = _print
return cls class Document:#假设为第三方库,不允许修改
def __init__(self,content):
self.content = content class Word(Document):pass#假设为第三方库,不允许修改
class Pdf(Document):pass#假设为第三方库,不允许修改 @printable
class PrintableWord(Word):pass print(PrintableWord.__dict__)#{'__module__': '__main__', '__doc__': None, 'print': <function printable.<locals>._print at 0x0000000002961730>}
PrintableWord('test').print()#test 装饰器

4.Mixin

#Mixin示例1
class PrintableMixin:
def print(self):
print(self.content,'Mixin') class Document:
def __init__(self,content):
self.content = content class Word(Document):pass
class Pdf(Document):pass class PrintableWord(PrintableMixin,Word):pass
print(PrintableWord.__dict__)
print(PrintableWord.mro()) pw = PrintableWord('test\nstring')
pw.print()

#Mixin示例2
class PrintableMixin:
def print(self):
print(self.content,'Mixin') class Document:
def __init__(self,content):
self.content = content class Word(Document):pass
class Pdf(Document):pass class SuperPrintableMixin(PrintableMixin):
def print(self):
print('~'*20)
super().print() #通过继承复用
print('~'*20) class SuperPrintablePdf(SuperPrintableMixin,Pdf):pass
print(SuperPrintablePdf.__dict__)
print(SuperPrintablePdf.mro()) spp = SuperPrintablePdf('super print pdf')
spp.print()

Mixin类

练习

#1.Shape基类,要求所有子类都必须提供面积的计算,子类有三角形、矩形、圆
import math class Shape:
@property
def area(self):
raise NotImplementedError('基类未实现') class Triangle(Shape):
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c @property
def area(self):
p = (self.a+self.b+self.c)/2
return math.sqrt(p*(p-self.a)*(p-self.b)*(p-self.c)) class Circle(Shape):
def __init__(self,radius):
self.radius = radius @property
def area(self):
return math.pi*self.radius**2 class Rectangle(Shape):
def __init__(self,width,height):
self.width = width
self.height = height @property
def area(self):
return self.width*self.height # shapes = [Triangle(3,4,5),Rectangle(3,4),Circle(4)]
# for s in shapes:
# print('The area of {} = {}'.format(s.__class__.__name__,s.area)) #2.圆类的数据可序列化
import json
import msgpack def mydump(cls):
def _dumps(self, t='json'):
if t == 'json':
return json.dumps(self.__dict__)
elif t == 'msgpack':
return msgpack.packb(self.__dict__)
else:
raise NotImplementedError('没有实现的序列化')
cls.dumps = _dumps
return cls #使用Mixin类
# class SerializableMixin:
# def dumps(self,t='json'):
# if t == 'json':
# return json.dumps(self.__dict__)
# elif t == 'msgpack':
# return msgpack.packb(self.__dict__)
# else:
# raise NotImplementedError('没有实现的序列化')
#
#使用装饰器
@mydump
class SerializableCircleMixin(Circle):pass scm = SerializableCircleMixin(1)
print(scm.area)#3.141592653589793
print(scm.__dict__)#{'radius': 1}
print(scm.dumps('json'))#{"radius": 1}

 作业

#单链表
class SingleNode:
#代表一个节点
def __init__(self,val,next=None):
self.val = val
self.next = None class LinkedList:
#容器类,某种方式存储一个个节点
def __init__(self):
self.head = None
self.tail = None def append(self,val):
node = SingleNode(val)
if self.head is None:#
self.head = node
self.tail = node
self.tail.next = node
self.tail = node def iternodes(self):
while self.head:
yield self.head.val
self.head = self.head.next
 #实现双向链表
class SingleNode:
#代表一个节点
def __init__(self,val,next=None,prev=None):
self.val = val
self.next = next
self.prev = prev def __repr__(self):
return str(self.val) class LinkedList:
#容器类,某种方式存储一个个节点
def __init__(self):
self.head = None
self.tail = None def append(self,val):
node = SingleNode(val)
if self.head is None:#
self.head = node
else:
self.tail.next = node
node.prev = self.tail
self.tail = node def pop(self):
if self.tail is None:#
raise NotImplementedError('Empty')
tail = self.tail
prev= self.tail.prev
if prev is None:#1个节点
self.head = None
self.tail = None
else:#>1
self.tail = prev
prev.next = None
return tail.val def insert(self,index,val):#1,7
if index < 0:
raise Exception('Error')
cur = None
for i,current in enumerate(self.iternodes()):
if i == index:
cur = current
break if cur is None:#说明索引越界或空链表,直接末尾追加
self.append(val)
return node = SingleNode(val)
prev = cur.prev
if prev is None:#1个节点,头部插入
self.head = node
node.next = cur
cur.prev = node
else:#>=2
node.next = cur
prev.next = node
cur.prev = node
node.prev = prev def iternodes(self,reversed = False):
current = self.head
while current:
yield current
current = current.next a = SingleNode(1)
b = SingleNode(2)
c = SingleNode(3)
d = SingleNode(4)
e = SingleNode(5)
f = SingleNode(6)
ll = LinkedList()
ll.append(a)
ll.append(b)
ll.append(c)
ll.append(d)
ll.append(e)
ll.append(f)
# ll.insert(1,0)
# ll.insert(0,0)
ll.insert(10,100)
print('pop元素:',ll.pop())
print('pop元素:',ll.pop())
print('pop元素:',ll.pop())
ll.insert(0,10) for node in ll.iternodes():
print(node)

Pyhon进阶9---类的继承的更多相关文章

  1. Python类的继承(进阶5)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6411918.html 本文出自:[Edwin博客园] Python类的继承(进阶5) 1. python中什 ...

  2. 苹果新的编程语言 Swift 语言进阶(十)--类的继承

    一.类的继承 类能够从其它类继承方法.属性以及其它特性,当一个类从另外的类继承时,继承的类称为子类,它继承的类称为超类.在Swift中,继承是类区别与其它类型(结构.枚举)的基础行为. 1.1 .类的 ...

  3. Java+7入门经典 - 6 扩展类与继承 Part 1/2

    第6章 扩展类与继承 面向对象编程的一个重要特性: 允许基于已定义的类创建新的类; 6.1 使用已有的类 派生 derivation, 派生类 derived class, 直接子类 direct s ...

  4. 游戏编程之Unity常用脚本类的继承关系

    前言学习Unity开发引擎的初学者会接触大量的脚本类,而这些类之间的关系往往容易被忽略.本文对Unity引擎开发中的一些常用类及其关系进行了简单的归纳总结. 博文首发地址:http://tieba.b ...

  5. Objective-C编程 — 类和继承

    讲述面向对象中的一个重要概念——继承,使用继承 可以方便地在已有类的基础上进行扩展,定义一个具有父 类全部功能的新类. 父类和子类 我们在定义一个新类的时候,经常会遇到要定义的新类是某个类的扩展或者是 ...

  6. Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式

    要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...

  7. C++学习笔记:07 类的继承与派生

    课程<C++语言程序设计进阶>清华大学 郑莉老师) 基本概念 继承与派生的区别: 继承:保持已有类的特性而构造新类的过程称为继承. 派生:在已有类的基础上新增自己的特性(函数方法.数据成员 ...

  8. UML类图(上):类、继承和实现

    面向对象设计 对于一个程序员来说,在工作的开始阶段通常都是别人把东西设计好,你来做.伴随着个人的成长,这个过程将慢慢变成自己设计一部分功能来实现,自己实现.如果要自己设计,无论是给自己看,还是给别人看 ...

  9. 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸

    类的继承案例解析,python相关知识延伸 作者:白宁超 2016年10月10日22:36:57 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给 ...

随机推荐

  1. Asp.Net Core 轻松学-从安装环境开始

    Asp.Net Core 介绍     Asp.Net Core是微软新一代的跨平台开发框架,基于 C# 语言进行开发,该框架的推出,意味着微软从系统层面正式进击 Linux 服务器平台:从更新速度开 ...

  2. 【设计模式+原型理解】第一章:使用Javascript来巧妙实现经典的设计模式

    刚开始学习设计模式之前,我是没想说要学习设计模式的,我只是因为想学习JS中的原型prototype知识,一开始我想JS中为什么要存在原型这个东西?于是慢慢通过原型而接触到设计模式,后来发现我这个过程是 ...

  3. .net core入门-发布及部署_异常(处理程序“aspNetCore”在其模块列表中有一个错误模块“AspNetCoreModuleV2")处理

    备注:本人使用开发工具:VS2017,.NET Core 2.2,其中VS2017原本自带2.1,我单独从官网下载了2.2的程序集安装包,但是没有下配套的运行环境,运行项目时出了一个问题. 以下是我在 ...

  4. 由于服务主机:DCOM服务进程占用过多CPU,导致系统卡死

    最近在使用电脑的时候,总是出现电脑死机,而且鼠标也是经常卡在那里不动了,开始以为是鼠标的问题,还换了个鼠标(飙泪中),这还是一个血的教训啊!!!之后打开任务管理器发现CPU占用已经达到100%,而且一 ...

  5. alias,data,系统定时开关机的基本操作

    1.修改命令提示符的格式,及每个字母所代表的功能,显示提示符格式输入echo $PS1PS1="[\u@\h \W]\$"\e 或\033启用颜色 \u当前用户 \h主机名简称 \ ...

  6. Oracle数据库的安装 【超详细的文图详解】

    Oracle简介Oracle Database,又名Oracle RDBMS,或简称Oracle.是甲骨文公司的一款关系数据库管理系统.它是在数据库领域一直处于领先地位的产品.可以说Oracle数据库 ...

  7. Python笔记-高阶函数

    1.函数式编程 函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量. 函数式编程的一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数! 传入函数 既然 ...

  8. SSM框架多数据源和AOP事务管理之间

  9. 详细QRCode生成二维码和下载实现案例

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using ThoughtWo ...

  10. (爬虫)requests库

    一.requests库简介 urllib库和request库的作用一样,都是服务器发起请求数据,但是requests库比urllib库用起来更方便,它的接口更简单,选用哪种库看自己. 如果没有安装过这 ...