python中的多重继承中的超级功能 [英] super function in Multiple inheritance in python

查看:70
本文介绍了python中的多重继承中的超级功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在python 3.4中编写了此代码并使用了类.我已使用super()函数在此代码中实现了多重继承.我想调用库类的 init ()函数.但是我无法,有人能告诉我这个错误吗?

I have written this code in python 3.4 and used classes. I have implemented multiple inheritance in this code with super() function. And i want to call the init() function of library class. But i am unable, can anyone tell me the mistake?

代码

class college:
    def __init__(self, rollno):
        print("Roll no:", rollno)

class library:
    def __init__(self, libcardno):
        print("Library card no:", libcardno)

class student(college, library):
    def __init__(self, name):
        print("Name:", name)
        super().__init__(5560)
        super().__init__(60)

输出

>>> obj = student("John")
Name: John
Roll no: 5560
Roll no: 60

只了解这个问题,它不是另一个的重复.

Just understand the question, it's not a duplicate of another one.

推荐答案

您可以直接调用相应父类的__init__方法.

You can directly call __init__ method of respective parent class.

class student(college, library):
    def __init__(self, name):
        print("Name:", name)
        college.__init__(self,5560)
        library.__init__(self,60)

这篇关于python中的多重继承中的超级功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆