我有两个类,我想要从另一个类继承。
这两个类已经定义了def __init__
。现在,我希望这两个类继承一个需要一系列参数(参数)的方法。但是在应该继承的2个类中,def __init__
会被覆盖。
我该如何解决这个问题?我阅读了Python与def __init__
中的构造函数。我可以为该方法创建一个自定义构造函数吗?我在课堂上很新。
编辑:
这是我正在尝试的内容。这是一个GUI项目。这是主要的课程:
class Error:
def __init__(self, check0, check1, check2, check3, check4, check5, check6, check7, check8, check9, check10, check11,
check12, check13, check14, check15):
self.check0 = check0
self.check1 = check1
self.check2 = check2
self.check3 = check3
self.check4 = check4
self.check5 = check5
self.check6 = check6
self.check7 = check7
self.check8 = check8
self.check9 = check9
self.check10 = check10
self.check11 = check11
self.check12 = check12
self.check13 = check13
self.check14 = check14
self.check15 = check15
def feedbackOnError(self):
feedback = ''
global feedback
if self.check0 == False:
feedback0 = "You didn't provide a name and/or mail"
feedback = feedback0
if self.check3 == False:
feedback3 = "You didn't provide a valid year (you used over 4 digits or none)"
feedback = feedback3
if self.check4 == False:
feedback4 = "You didn't provide a valid year (you used letters)"
feedback = feedback4
if self.check5 == False:
feedback5 = "You didn't provide a valid month (you used over 2 digits or none)"
feedback = feedback5
if self.check6 == False:
feedback6 = "You didn't provide a valid month (you used letters)"
feedback = feedback6
if self.check7 == False:
feedback7 = "You didn't provide a valid month (you used a number above 12)"
feedback = feedback7
if self.check8 == False:
feedback8 = "You didn't provide a valid month '00' is not a month)"
feedback = feedback8
if self.check9 == False:
feedback9 = "You didn't provide a valid day (you used letters)"
feedback = feedback9
if self.check10 == False:
feedback10 = "You didn't provide a valid day '00' is not a day)"
feedback = feedback10
if self.check11 == False:
feedback11 = "You didn't provide a valid day (you used a number above 31)"
feedback = feedback11
if self.check12 == False:
feedback12 = "You didn't provide a valid day (you used over 2 digits or none)"
feedback = feedback12
if self.check13 == False:
feedback13 = "Your month doesn't match up with your day (February has 29 during leap year)"
feedback = feedback13
if self.check14 == False:
feedback14 = "Your month doesn't match up with your day (February has 28 during normal years)"
feedback = feedback14
if self.check15 == False:
feedback15 = "Your chosen month and day don't match up (You used '31' for a month with only 30)"
feedback = feedback15
if self.check1 == False:
feedback1 = "You used characters that are not permitted: \n / ' * \\ _ > < [ ] ( ) | + { } % & ! \" # = ? ^ ~ . , ; :"
feedback = feedback1
if self.check2 == False:
feedback2 = "You used characters that are not permitted: \n / ' * \\ _ > < [ ] ( ) | + { } % & ! \" # = ? ^ ~ . , ; :"
feedback = feedback2
这是我想要继承主类的类:
class AddDialog(sized_controls.SizedDialog, Error):
def __init__(self, *args, **kwargs):
super(AddDialog, self).__init__(*args, **kwargs)
class EditDialog(sized_controls.SizedDialog, Error):
def __init__(self, *args, **kwargs):
super(EditDialog, self).__init__(*args, **kwargs)
答案 0 :(得分:0)
问题似乎是你只执行了你拥有的两个基类__init__
方法之一,但你想传递参数并调用它们。
super
将只显示方法解析顺序(MRO)中首先出现的方法,因此您必须至少调用其中一个__init__
s“手动”。您还必须决定派生__init__
的哪些参数去哪里,因为您必须传入两组参数。为了这个例子,我假装错误只需要三个必需的位置参数(check0
,check1
和check2
):
class AddDialog(sized_controls.SizedDialog, Error):
def __init__(self, check0, check1, check2, *args, **kwargs):
Error.__init__(self, check0, check1, check2)
SizedDialog.__init__(self, *args, **kwargs)
在相关的说明中,我建议您重构代码以使用列表,因为老实说,16个参数是荒谬的。使用*args
,您可以避免重构太多额外的代码:
class Error:
feedback_strings = [
"You didn't provide a name and/or mail",
"You used characters that are not permitted: \n / ' * \\ _ > < [ ] ( ) | + { } % & ! \" # = ? ^ ~ . , ; :",
"You used characters that are not permitted: \n / ' * \\ _ > < [ ] ( ) | + { } % & ! \" # = ? ^ ~ . , ; :",
"You didn't provide a valid year (you used over 4 digits or none)",
"You didn't provide a valid year (you used letters)",
# You get the idea...
]
def __init__(self, *checks):
if len(checks) != 16:
raise ValueError('16 positional arguments are required')
self.checks = [bool(x) for x in checks]
def feedbackOnError(self):
for index, check in enumerate(self.checks):
if not check:
return self.feedback_strings[index]
此代码比现有代码短10倍,易于维护且易于阅读。我还建议从反馈函数返回一个值,而不是设置一个全局变量。全局变量方法是一个非常糟糕的主意,因为你真的不知道谁会修改它。