不确定Form .__ init __(self)做什么?

时间:2019-08-14 08:23:32

标签: python .net forms ironpython init

我正在查看一些代码,以便使用Windows窗体使用IronPython制作选项卡式拆分图像查看器,并且 init 函数中有一行我不理解,看不到何时的解释我在Google上搜索了它。我在相关行的旁边添加了一条评论。

下面是一些代码,只是样板代码会显示一个空表格。

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函数中的行仍然相同,有人知道它的作用吗?

1 个答案:

答案 0 :(得分:1)

MainForum类是Form类的扩展。

Form.__init__(self)所做的所有事情都是调用Form类的构造函数。

小例子: 让我们将人类和学生分成两个班。一个人有名字,这就是他所做的。学生是人类,但具有其他属性,例如他访问的学校。他还可以告诉你他的名字。

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()   

输出:     约翰·杜(John Doe)

您可以想到它,就像Parent类现在是Subclass的一部分。一个学生仍然在一个人里面。