我们可以传递给 super() 什么参数? [英] What argument can we pass to super()?
问题描述
我创建了一个 Vehicle
类,并希望从它派生一个 Car
类,它调用父构造函数来设置 name
和 <代码>颜色代码>.但是我收到此错误:
I created a Vehicle
class and want to also have a Car
class derived from it that calls the parent constructor to set name
and color
. However I'm getting this error:
super() takes at least 1 argument (0 given)
这是我的代码:
class Vehicle:
def __init__(self, name, color):
self.__name = name # __name is private to Vehicle class
self.__color = color
def getColor(self): # getColor() function is accessible to class Car
return self.__color
def setColor(self, color): # setColor is accessible outside the class
self.__color = color
def getName(self): # getName() is accessible outside the class
return self.__name
self.__model = model
def getDescription(self):
return self.getName() + self.__model + " in " + self.getColor() + " color"
class Car(Vehicle):
def __init__(self, name, color, model):
# call parent constructor to set name and color
super().__init__(name, color)
self.__model = model
def getDescription(self):
return self.getName() + self.__model + " in " + self.getColor() + " color"
# in method getDescrition we are able to call getName(), getColor() because they are
# accessible to child class through inheritance
c = Car("Ford Mustang", "red", "GT350")
print(c.getDescription())
推荐答案
Python 3 - nice
在 Python 3 中这是有效的:
Python 3 - nice
In Python 3 this works:
class Vehicle:
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super().__init__()
car = Car()
印刷品:
Vehicle __init__() called
Python 2 - 更多工作
在 Python 2 中尝试同样的事情会导致问题:
Python 2 - more work
Trying the same thing in Python 2 causes problems:
class Vehicle:
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super().__init__()
car = Car()
抛出这个异常:
Traceback (most recent call last):
...
TypeError: super() takes at least 1 argument (0 given)
我们需要提供自己的类作为第一个和 self
作为 super()
的第二个参数:
We need to supply the own class as first and and self
as second argument to super()
:
class Vehicle:
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super(Car, self).__init__()
car = Car()
但这还不够:
Traceback (most recent call last):
...
TypeError: must be type, not classobj
class Vehicle:
创建一个旧式的类.Vehicle
必须从 object
继承以获得一个与 super()
一起使用的新式类:
class Vehicle:
creates an old-style class. Vehicle
has to inherit from object
to get a new-style class that works with super()
:
class Vehicle(object):
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super(Car, self).__init__()
car = Car()
印刷品:
Vehicle __init__() called
super() 在 Python 2 中没有参数
必须一直记住这两个论点有点烦人.幸运的是,有一个解决方案.强烈推荐的库 Python-Future 允许您在 Python 中不带参数地使用 super()
2:
super() without arguments in Python 2
Having to remember these two arguments all the time is kind of annoying. Fortunately, there is a solution. The highly recommended library Python-Future allows you to use super()
without arguments in Python 2:
from builtins import object, super # from Python-Future
class Vehicle(object):
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super().__init__()
car = Car()
印刷品:
Vehicle __init__() called
这篇关于我们可以传递给 super() 什么参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!