Python多继承顺序实例分析

yipeiwu_com6年前Python基础

本文实例讲述了Python多继承顺序。分享给大家供大家参考,具体如下:

示例1:

#-*- coding:utf-8 -*-
#!python2
class A(object):
  def caller(self):
    print 'A caller'
    self.called()
  def called(self):
    print 'A called'
class B(object):
  def called(self):
    print 'B called'
class C(B,A):
  pass
if __name__ == '__main__':
  c=C()
  c.caller()

运行结果:

A caller
B  called

示例2:

#-*- coding:utf-8 -*-
#!python2
class A(object):
  def caller(self):
    print 'A caller'
    self.called()
  def called(self):
    print 'A called'
class B(object):
  def called(self):
    print 'B called'
class C(A,B):
  pass
if __name__ == '__main__':
  c=C()
  c.caller()

运行结果:

A caller
A called

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

Windows10下 python3.7 安装 facenet的教程

Windows10下 python3.7 安装 facenet的教程

前提 1.python环境及tensorflow安装成功 2.Anaconda安装好 ,Anaconda安装步骤 安装步骤 1.下载facenet,https://github.com/...

对python实现模板生成脚本的方法详解

最近项目需要,针对主项目提取一个小的基础版本,供于在新建项目时使用,所以就有这个python模板生成脚本,其作用如下: 1、通过配置文件来控制模板中的数据、格式化的过滤条件 2、执行后会...

python3.6使用tkinter实现弹跳小球游戏

本文实例为大家分享了python3.6实现弹跳小球游戏的具体代码,供大家参考,具体内容如下 import random import time from tkinter import...

python实现五子棋游戏(pygame版)

python实现五子棋游戏(pygame版)

本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下 目录 简介 实现过程 结语 简介 使用python实现pygame版的五子棋游戏...

Python实现计算长方形面积(带参数函数demo)

Python实现计算长方形面积(带参数函数demo)

如下所示: # 计算面积函数 def area(width, height): return width * height def print_welcome(name):...