编写一个名为Person的类,其中包含人名,地址,电话和电子邮件的数据属性。接下来,编写一个名为Customer的类,它是Person类的子类。 Customer类应具有客户编号的数据属性和布尔数据属性,指示客户是否希望成为邮件列表中的一个。在一个简单的程序中演示Customer类的实例。
我收到了一个属性错误。
, line 6, in main
'555-987-1549','adsf@asd.com','Y','1153')
TypeError: __init__() takes 5 positional arguments but 7 were given
我没有完全遵循使用布尔表达式的观点,所以也许有人可以解释它为什么被使用并帮助我解决为什么我会收到该错误。
继承我的代码
班级Person
:
class Person:
def __init__(self, name, address, telephone, email):
self.__name = name
self.__address = address
self.__telephone = telephone
self.__email = email
def set_name(self, name):
self.__name = name
def set_address(self, address):
self.__address = address
def set_telephone(self, telephone):
self.__telephone = telephone
def set_email(self, email):
self.__email = email
def get_name(self):
return self.__name
def get_address(self):
return self.__address
def get_telephone(self):
return self.__telephone
def get_email(self):
return self.__email
班级Customer
:
import Person
class Customer(Person.Person):
def __init__(self, name, address, telephone, email):
Person.Person.__init__(self, mail, number)
self.mailing = 'Y'== True
self.__mail = mail
self.__number = number
def set_mail(self, mail):
self.__mail = mail
def set_number(self, number):
self.__number = number
def get_mail(self):
return self.__mail
def get_number(self):
return self.__number
def mailList(self, mail):
if mailing == True:
return"On the mailing list"
else:
return"Not on the mailing list"
def __str__(self):
return "\nName: {}\nAddress: {}\ntelephone: {}\nEmail: {}\nMail: {}\nNumber: {}".\
format(self.get_name(), self.get_address(),\
self.get_telephone(), self.get_email()\
, self.mailList(self.get_mail()),self.get_number())
customerTest
:
import Customer
def main():
customer = Customer.Customer('Josh', 'Long st, Dallas TX',\
'555-987-1549','adsf@asd.com','Y','1153')
print(customer)
main()
答案 0 :(得分:1)
看起来不是
def __init__(self, name, address, telephone, email):
你的意思是:
def __init__(self, name, address, telephone, email, mail, number):
顺便问一下,这里发生了什么?
self.mailing = 'Y'== True
你可能意味着
self.mailing = mail == True
编辑:是的,托比亚斯说。请修复此问题,包括缺少参数 -
Person.Person.__init__(self, mail, number)
此外,虽然没有必要,you might want to使用super().(...)
语法而不是直接调用Person.__init__(self, ...)
。
答案 1 :(得分:0)
__init__
构造函数不正确,参数数量不正确。
在self.mailing
中,根据参数,您可能需要True
或False
。
def __init__(self, name, address, telephone, mail, mailing, number):
Person.Person.__init__(self, name, address, telephone, mail)
self.mailing = True if mailing == 'Y' else False
self.__mail = mail
self.__number = number
同样mailList
需要使用self来引用当前对象。
def mailList(self, mail):
if self.mailing == True:
return"On the mailing list"
else:
return"Not on the mailing list"
答案 2 :(得分:0)
对于Person Class,如果您使用的是Python2.7,请将其定义为继承自Object
类,以便以后能够正确使用super
表达式,否则如果您处理此类Python3 +,你可以说super().__init__()
,所以:
人员类将是:
class Person(object):#only change
def __init__(self, name, address, telephone, email):
self.__name = name
self.__address = address
self.__telephone = telephone
self.__email = email
def set_name(self, name):
self.__name = name
def set_address(self, address):
self.__address = address
def set_telephone(self, telephone):
self.__telephone = telephone
def set_email(self, email):
self.__email = email
def get_name(self):
return self.__name
def get_address(self):
return self.__address
def get_telephone(self):
return self.__telephone
def get_email(self):
return self.__email
至于客户类,请阅读我做出更改的代码注释
import Person
class Customer(Person.Person):
def __init__(self, name, address, telephone, email, mail, number): #fixed number of arguments to pass for class instantiation
super(type(self), self).__init__(name, address, telephone, email) #Also here and not the use of `super`
#super().__init__(name, address, telephone, email) with Python 3+
self.mailing = True if mail == 'Y' else False #Fixed self.mailing expression
self.__mail = mail
self.__number = number
def set_mail(self, mail):
self.__mail = mail
def set_number(self, number):
self.__number = number
def get_mail(self):
return self.__mail
def get_number(self):
return self.__number
def mailList(self): #No need for mail argument
if self.mailing == True: #Just Check here self.mailing
return "On the mailing list"
else:
return "Not on the mailing list"
def __str__(self):
return "\nName: {}\nAddress: {}\ntelephone: {}\nEmail: {}\nMail: {}\nNumber: {}".\
format(self.get_name(), self.get_address(),\
self.get_telephone(), self.get_email()\
, self.mailList(),self.get_number())