继承

一个类可以派生出一个子类,这个子类可以使用父类的属性及方法,也可以在父类的基础上添加自己的独特属性或方法。属性和方法的继承的顺序是先从自己开始,找不到再去找父类,父类没有再找父类的父类,其尽头就是顶级基类object,它就相当于一个人的祖宗。当一个类没有写继承谁时,默认就是继承object。

class father(object):

def __init__(self):

self.name = "wang"

self.__age = 20

def A(self):

print(self.__age)

class son(father):

pass

a = son()

print(a.name)

a.A()

wang

20

上面代码中,son类继承了father类,所以可以直接使用父类的属性和方法。

多继承

一个类可以同时继承多个类,这个就是类的多继承。当继承多个父类时,如果父类中有相同的方法,那么子类会优先使用最先被继承的方法。下面的例子可以明显地看出来。

class father():

def __init__(self):

self.name = "wang"

self.__age = 20

def A(self):

print("this is father\'s method")

class mother():

def __init__(self):

self.sex = "man"

def A(self):

print("this is mother\'s method")

class son(father,mother):

pass

a = son()

a.A()

this is father's method

当子类不想调用父类的方法,可以通过重写来覆盖父类的方法。

class father():

def __init__(self):

self.name = "wang"

self.__age = 20

def A(self):

print("this is father\'s method")

class mother():

def __init__(self):

self.sex = "man"

def A(self):

print("this is mother\'s method")

class son(father,mother):

def A(self):

print("this is my method")

a = son()

a.A()

this is my method

当子类重写父类方法后,若想再次调用父类方法,可以使用super()方法。还可以调用类的__mro__属性(返回元组)或mro方法(返回列表)来获取类的继承关系。

class father():

def __init__(self):

self.name = "wang"

self.__age = 20

def A(self):

print("this is father\'s method")

class mother():

def __init__(self):

self.sex = "man"

def A(self):

print("this is mother\'s method")

class son(father,mother):

def A(self):

super().A()

a = son()

a.A()

print(son.mro())

print(son.__mro__)

this is father's method

[, , , ]

(, , , )

魔术方法

类的魔术方法有很多,所谓的魔术方法就是不需要自己调用,就可以自己再特定的时刻被自动调用,魔术方法的形式时__name__()。最常见的魔术方法就是初始化和析构了,下面介绍几种另外的魔术方法。

运算方法:运算方法可以实现类之间的运算,方便类之间的操作。

__add__(self,other) #x + y

__sub__(self,other)#x - y

__mul__(self,other)#x * y

__mod__(self,other)#x % y

__iadd__(self,other)# x += y

__isub__(self,other)# x -= y

__radd__(self,other)#y + x

__rsub__(self,other)#y - x

__imul__(self,other)#x *= y

__imod__(self,other)#x %= yclass A():

def __init__(self):

self.number = 20

def __add__(self,other):

return self.number + other.number

def __sub__(self,other):

return self.number - other.number

class B():

def __init__(self):

self.number = 30

a = A()

b = B()

print (a+b)

print (a-b)

50

-10

__call__方法:正常情况下,实例是不能像函数一样被调用的,但通过写__call__方法就能做到。

class A():

def __init__(self):

self.number = 20

def __call__(self):

print("this call method")

a = A()

a()

this call method

类中一些查询相关信息的方法,这些方法基本了解就行,实际中并不常用。

__class__  格式:实例.__class__

__dict__     格式:实例.__dict__

__doc__     格式:类名.__doc__

__bases__  格式:类名.__bases__

__mro__     格式:子类名.__mro__

class father():

def __init__(self):

self.name = "wang"

self.__age = 20

def A(self):

print("this is father\'s method")

class mother():

def __init__(self):

self.sex = "man"

def A(self):

print("this is mother\'s method")

class son(father,mother):

def A(self):

super().A()

a = son()

print (a.__class__)#查看类名

print (a.__dict__)#查看全部属性,返回属性和属性值键值对形式

print (son.__doc__)#查看对象文档

print (son.__bases__)#查看父类

print (son.__mro__)

{'name': 'wang', '_father__age': 20}

None

(, )

(, , , )

python 类继承方法_python类的继承、多继承及其常用魔术方法相关推荐

  1. invoke 魔术_PHP常用魔术方法(__invoke魔术方法)

    PHP5.3魔术方法 __invoke 这个魔幻方法被调用的时机是: 当一个对象当做函数调用的时候, 如果对象定义了__invoke魔幻方法则这个函数会被调用, class Callme { publ ...

  2. python类的继承super方法_Python类的继承super相关原理解析

    看了网上许多关于super.mro.C3的介绍感觉没有一份很容易初学者理解的文档,直接看C3算法的话,比较难理解,也没必要,如果掌握一套规律的话,会轻松许多.我将网上这些博主的文章进行一个梳理总结,最 ...

  3. python定义一个人类_Python类的定义、继承及类对象使用方法简明教程

    Python编程中类的概念可以比作是某种类型集合的描述,如"人类"可以被看作一个类,然后用人类这个类定义出每个具体的人--你.我.他等作为其对象.类还拥有属性和功能,属性即类本身的 ...

  4. python类继承实例_python类继承与子类实例初始化用法分析

    这篇文章主要介绍了python类继承与子类实例初始化用法,实例分析了Python类的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下 [ 先贴参考书籍原文(中文英文对照)] __init__方法介 ...

  5. python类的继承优缺点_python 类的三大特性--继承

    继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题, 继承是一种创建新类的方式,在python中新建的类可以继承一个或多个父类,父 ...

  6. python类中方法调用自己类的方法_python 类中方法总结 --- 实例方法、类方法、静态方法...

    在python的类语法中,可以出现三种方法,具体如下: (1)实例方法 1)第一个参数必须是实例本身,一般使用[self]表示. 2)在实例方法中,可以通过[self]来操作实例属性,[类名]来操作类 ...

  7. python 类的内置方法_Python 类的常用内置方法

    类的内置方法(魔法方法): 凡是在类内部定义,以__开头__结尾的方法,都是类的内置方法,类的内置方法,会在满足某种条件下自动触发. 1.1__new__ __new__:在___init__触发前, ...

  8. python类与函数编程_Python类三种方法,函数传参,类与实例变量(一)详解

    1 Python的函数传递: 首先所有的变量都可以理解为内存中一个对象的'引用' a = 1 def func(a): a = 2 func(a) print(a) # 1 a = 1 def fun ...

  9. python类的专有方法_Python——类的专有方法

    Python除了自定义私有变量和方法外,还可以定义专有方法.专有方法是在特殊情况下或使用特殊语法时由python调用的,而不是像普通方法一样在代码中直接调用.看到形如__XXX__的变量或函数名时就需 ...

最新文章

  1. SAP 全球产品营销总监:产品营销驱动 B2B 企业爆发式增长的 6 大要点
  2. ansible-01
  3. SQL注入学习——时间盲注详解 sqli-labs(Less 9)
  4. stm32c语言设计以及注释,13个基于STM32的经典项目设计实例,全套资料~-嵌入式系统-与非网...
  5. 概率论 —— 数学期望
  6. 2018 5大技术趋势
  7. Spring Boot 学习之,数据库三 ,事务
  8. vscode打开自动提示_Python编程的最好搭档:VSCode 详细指南
  9. 阿里云边缘计算三年,都为开发者带来了什么?
  10. 心理正常与异常的区分_医学心理学:如何区分正常心理和异常心理?
  11. Linux高性能server规划——多线程编程(在)
  12. DXUT框架剖析(8)
  13. php 孙中岳_华杯成绩终于出来了!(学而思、华英)
  14. css如何使文字抖动,CSS美化:实现抖音彩色文字抖动效果
  15. 计算机输入d为啥返回桌面,按D键空格键就退出输入回到桌面
  16. Selenium学习 - WebElement接口
  17. 自控力读书笔记:第二章 意志力的本能:人生来就能地址奶酪蛋糕的诱惑
  18. 机器学习python中train_test_split()函数进行数据集分割
  19. NS2 队列管理机制
  20. Holder不等式 Minkowski不等式

热门文章

  1. BZOJ5466 NOIP2018保卫王国(倍增+树形dp)
  2. 分页技术与JDBC一览
  3. [中英对照]The sysfs Filesystem | sysfs文件系统
  4. pc端实现 网页居中显示 且自适应
  5. 【转】Java垃圾收集器
  6. hibernate分页中跳转到第几页的功能
  7. Oracle Hint
  8. pku2750 Potted Flower
  9. unixodbc mysql安装_ubuntu12下安装unixODBC(mysql)
  10. 初学者python笔记(re模块、正则表达式完全解析)