如果我有一个python类:
class BaseClass(object):
#code and the init function of the base class
Run Code Online (Sandbox Code Playgroud)
然后我定义了一个子类,例如:
class ChildClass(BaseClass):
#here I want to call the init function of the base class
Run Code Online (Sandbox Code Playgroud)
如果基类的init函数接受一些我将它们作为子类的init函数的参数的参数,我该如何将这些参数传递给基类?
我写的代码是:
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
class ElectricCar(Car):
def __init__(self, battery_type, model, color, mpg):
self.battery_type=battery_type
super(ElectricCar, self).__init__(model, color, mpg)
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
我问的原因是纯粹的好奇心.我可能会看到,如果你事先不知道你想要应用于某些变量的操作,或者在递归调用的某个级别应用不同的操作,或者它可能只是使某些事情更容易和/或更整洁.
虽然我只是猜测,但这可能是一个非常糟糕的主意,但整体而言只是好奇.
所以我不知道你们有没有/曾经去过NewBoston.Com(顺便说一句,这是一个很好的资源),但是我正在观看这个视频,老师说你如何使用公共功能访问私人信息. .
这是视频:http: //www.youtube.com/watch?v = jTS7JTud1qQ
当然,只需跳到最后就可以了解它的要点.
我不知道的是,如何使用公共功能访问私人信息.如果你创建一个公共的功能,允许任何人设置名称,并获得该名称,那么它不仅仅是公共的吗?,任何人都不能弄乱我的代码和我想要的输出?
python ×2
c++ ×1
constructor ×1
function ×1
inheritance ×1
methods ×1
private ×1
public ×1
python-2.7 ×1
python-3.x ×1