python 带参数的多重继承

简介: 1. 不带参数的多重继承# 作者:hhh5460# 时间:2017.07.18class A(object): def show_x(self): print('A') class B(object): def show_y...

1. 不带参数的多重继承

# 作者:hhh5460
# 时间:2017.07.18

class A(object):
    def show_x(self):
        print('A')
        
        
class B(object):
    def show_y(self):
        print('B')
        
        
class C(object):
    def show_z(self):
        print('C')
        
        
class D(A, B, C):
    pass
        
        
# 测试
if __name__ == '__main__':
    d = D()
    d.show_x() # A
    d.show_y() # B
    d.show_z() # C

2. 带参数的多重继承

# 作者:hhh5460
# 时间:2017.07.18

class A(object):
    def __init__(self, x=0):
        self._x = x
    
    def show_x(self):
        print(self._x)
        
    def show_name(self):
        print('A')
        
        
class B(object):
    def __init__(self, y=0):
        self._y = y
    
    def show_y(self):
        print(self._y)
        
    def show_name(self):
        print('B')
        
        
class C(object):
    def __init__(self, z=0):
        self._z = z
    
    def show_z(self):
        print(self._z)
        
    def show_name(self):
        print('C')
        
# 注意下面两类D、E,都是继承A、B、C,且A类的优先级最高。但是三条__init__语句的顺序是相反的
class D(A, B, C):
    def __init__(self, x=0, y=0, z=0):
        C.__init__(self, z) # init C
        B.__init__(self, y) # init B
        A.__init__(self, x) # init A (A最优先)
        
class E(A, B, C):
    def __init__(self, x=0, y=0, z=0):
        super(E, self).__init__(x) # init A (A最优先)  # 此句可简写成:super().__init__(x)
        super(A, self).__init__(y) # init B
        super(B, self).__init__(z) # init C
        
        
        
# 测试
if __name__ == '__main__':
    d = D(1,2,3)
    d.show_x()    # 1
    d.show_y()    # 2
    d.show_z()    # 3
    d.show_name() # A
    
    e = E(1,2,3)
    e.show_x()    # 1
    e.show_y()    # 2
    e.show_z()    # 3
    e.show_name() # A
目录
相关文章
|
Python
【透彻】Python装饰器进阶(类装饰器+带参数的装饰器+多装饰器)| Python 主题月
【透彻】Python装饰器进阶(类装饰器+带参数的装饰器+多装饰器)| Python 主题月
146 0
|
Python 关系型数据库 MySQL
|
Python
Python - 带参数的方法
import math class Point: def move(self, x, y): self.x = x self.y = y def reset(self): self.
787 0
|
9天前
|
存储 人工智能 数据处理
Python:编程的艺术与科学的完美交融
Python:编程的艺术与科学的完美交融
14 1
|
4天前
|
测试技术 调度 索引
python编程中常见的问题
【4月更文挑战第23天】
16 2
|
5天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver
|
5天前
|
机器学习/深度学习 数据挖掘 算法框架/工具
Python:编程的艺术与魅力
Python:编程的艺术与魅力
16 3
|
5天前
|
机器学习/深度学习 数据可视化 数据挖掘
实用技巧:提高 Python 编程效率的五个方法
本文介绍了五个提高 Python 编程效率的实用技巧,包括使用虚拟环境管理依赖、掌握列表推导式、使用生成器提升性能、利用装饰器简化代码结构以及使用 Jupyter Notebook 进行交互式开发。通过掌握这些技巧,可以让你的 Python 编程更加高效。
|
6天前
|
算法 Python
Python面向对象oop编程(二)
Python面向对象oop编程(二)
|
8天前
|
机器学习/深度学习 数据挖掘 API
pymc,一个灵活的的 Python 概率编程库!
pymc,一个灵活的的 Python 概率编程库!
16 1