不确定Form.__init__(自我)是做什么的?



我正在查看一些使用 Windows 表单使用 IronPython 制作选项卡式拆分图像查看器的代码,init函数中有一行我不明白,当我谷歌它时看不到解释。我在有问题的行旁边发表了评论。

下面是一些代码,它只是样板代码,会弹出一个空表单。

import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import Application, Form
class MainForm(Form):
def __init__(self):
Form.__init__(self) #what is this line doing?
self.Show()
Application.EnableVisualStyles()
form = MainForm()
Application.Run(form)

在页面上的其他地方,http://www.voidspace.org.uk/ironpython/winforms/part11.shtml 它有一个完成的程序,该程序可以正常工作(当您添加额外的图像时,选项卡什么也不做(,但在 init 函数中仍然有相同的行,有人叩头它做什么吗?

类 MainForum 是类 Form 的扩展。

Form.__init__(self)所做的只是调用 Form 类的构造函数。

小例子: 让我们做2个类人类和学生。人类有一个名字,这就是他所做的一切。学生是人类,但具有其他属性,例如他访问的学校。他也能告诉你他的名字。

class Human():
def __init__(self, name):
self.name = name #We set the name of the human
class Student(Human):
def __init__(self, name, school):
self.school = school
Human.__init__(self, name) #We set the name of the Human inside of the Person
def tellName(self):
print(self.name)
student1 = Student("John Doe","ETH Zurich")
student1.tellName()   

输出: 无名氏

你可以把它想象成父类现在是子类的一部分。一个学生在里面仍然是一个人。

最新更新