2024-05-29 00:04:28 发布
网友
假设类A有一个重载的init?如果init包含多个参数集,如何在第一个地方重载init?在
另外,当扩展到子类时,如何确保子类init与重载的父类init兼容?在
Python不支持函数重载,因为它是一种动态语言,请参见: Overloaded functions in python?
如果我理解正确,您需要super从子类调用基类的方法:
super
In [95]: class Base(object): ...: def __init__(self, x): ...: print 'in Base.ctor, x is:', x ...: ...: class Child(Base): ...: def __init__(self, x, y): ...: super(Child, self).__init__(x) In [96]: c = Child(1,2) in Base.ctor, x is: 1
请参见:Understanding Python super() with __init__() methods
Python不支持函数重载,因为它是一种动态语言,请参见: Overloaded functions in python?
如果我理解正确,您需要
super
从子类调用基类的方法:请参见:Understanding Python super() with __init__() methods
相关问题 更多 >
编程相关推荐