不确定 Form.__init__(self) 做什么?
Not sure what Form.__init__(self) does?
我正在查看一些使用 IronPython 使用 windows 表单制作选项卡式拆分图像查看器的代码,并且 init 函数中有一行我不知道当我 google it.I 在相关行旁边放置评论时,我不明白也看不到解释。
下面是一些代码,它只是样板代码,将显示一个空表单。
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 函数中仍然有相同的行,有人知道它是什么吗是吗?
class MainForum 是 class 表单的扩展。
所有 Form.__init__(self)
所做的,就是调用表单 class 的构造函数。
小例子:
让我们制作 2 classes Human 和 Student。一个人有一个名字,这就是他所做的一切。学生是人,但具有其他属性,例如他访问过的学校。他还能告诉你他的名字。
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()
输出:
李四
你可以把它想象成 Parent class 现在是 Subclass 的一部分。一个学生里面仍然是一个人。
我正在查看一些使用 IronPython 使用 windows 表单制作选项卡式拆分图像查看器的代码,并且 init 函数中有一行我不知道当我 google it.I 在相关行旁边放置评论时,我不明白也看不到解释。
下面是一些代码,它只是样板代码,将显示一个空表单。
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 函数中仍然有相同的行,有人知道它是什么吗是吗?
class MainForum 是 class 表单的扩展。
所有 Form.__init__(self)
所做的,就是调用表单 class 的构造函数。
小例子: 让我们制作 2 classes Human 和 Student。一个人有一个名字,这就是他所做的一切。学生是人,但具有其他属性,例如他访问过的学校。他还能告诉你他的名字。
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()
输出: 李四
你可以把它想象成 Parent class 现在是 Subclass 的一部分。一个学生里面仍然是一个人。