码迷,mamicode.com
首页 > 其他好文 > 详细

015: class and objects > 多重继承

时间:2016-01-19 00:10:07      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

1. 简单多重继承:

多重继承一个比较麻烦的问题就是父类的初始化的问题,下面这个例子算是一般比较常见的方式。但是这种方式存在着一个问题,那就是object的初始化函数将运行两次,一般情况下这不会产生什么问题,但是有时候可能会引起设计上的致命缺陷。

class Mother(object):
    def __init__(self, first_name):
        self.first_name = first_name

class Father(object):
    def __init__(self, last_name):
        self.last_name = last_name

class Child(Mother, Father):
    def __init__(self, first_name, last_name):
        Mother.__init__(self, first_name)
        Father.__init__(self, last_name)

Tom = Child(Tom, Jim)    

print(Tom.first_name, Tom.last_name)

 

015: class and objects > 多重继承

标签:

原文地址:http://www.cnblogs.com/jcsz/p/5140680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!