gpt4 book ai didi

python - 属性错误人员和客户类别

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:48 26 4
gpt4 key购买 nike

Write a class named Person with data attributes for a person's name, address, telephone and email. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be one the mailing list. Demonstrate an instance of the Customer class in a simple program.

我收到属性错误。

, line 6, in main
'555-987-1549','adsf@asd.com','Y','1153')
TypeError: __init__() takes 5 positional arguments but 7 were given

我没有完全遵循使用 bool 表达式的要点,所以也许有人可以解释为什么使用它并帮助我解释为什么会出现该错误。

这是我的代码

:

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

客户:

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

客户测试:

import Customer

def main():

customer = Customer.Customer('Josh', 'Long st, Dallas TX',\
'555-987-1549','adsf@asd.com','Y','1153')
print(customer)

main()

最佳答案

看起来像而不是

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, ...)

关于python - 属性错误人员和客户类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30965310/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com