码迷,mamicode.com
首页 > 编程语言 > 详细

python继承

时间:2018-07-01 18:59:28      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:自动   class   def   odi   nbsp   技术分享   ima   构造函数   类的构造函数   

继承的时候,调用父类的构造函数的方法:使用super()函数,只需要调用一次就行,会自动调用所有父类和父类的父类的构造函数

# -*- coding: utf-8 -*-
class A(object):
    def __init__(self):
        super(A, self).__init__()
        print("A__init__")

class B(object):
    def __init__(self):
        super(B, self).__init__()
        print("B__init__")

class C(A, B):
    def __init__(self):
        super(C, self).__init__() # 等价 super().__init__()
        print("C__init__")

x = C()

  

 输出为:

 技术分享图片

 

python继承

标签:自动   class   def   odi   nbsp   技术分享   ima   构造函数   类的构造函数   

原文地址:https://www.cnblogs.com/ACGame/p/9093734.html

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