Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从子类调用时的AttributeError_Python - Fatal编程技术网

Python 从子类调用时的AttributeError

Python 从子类调用时的AttributeError,python,Python,当我从子类调用其中一个方法(get.\u custnum或get\u mail)时,我收到一个属性错误,表示该对象没有名为subclass\u attribute的属性 我检查过,不只是get_custnum不起作用,get_mail仍然存在问题 在我的子类中,从超类调用方法没有问题。 在我的主函数中,在一个单独的文件中。 运行主函数时出错。 如果他们选择加入邮件列表,程序应显示姓名、地址、电话号码、客户号码和消息。我的输出是姓名、地址和电话号码(都来自超类),但不是客户号码和邮件列表消息(

当我从子类调用其中一个方法(get.\u custnum或get\u mail)时,我收到一个属性错误,表示该对象没有名为subclass\u attribute的属性

我检查过,不只是get_custnum不起作用,get_mail仍然存在问题

在我的子类中,从超类调用方法没有问题。 在我的主函数中,在一个单独的文件中。

运行主函数时出错。
如果他们选择加入邮件列表,程序应显示姓名、地址、电话号码、客户号码和消息。我的输出是姓名、地址和电话号码(都来自超类),但不是客户号码和邮件列表消息(来自子类)。

self.set\u custnum=custnum
没有调用该方法。您需要在
\uuuu init\uuuu
中调用该方法:

class Customer(Person):

    def __init__(self, name, address, telnum, custnum, mail):
        Person.__init__(self, name, address, telnum)

        self.set_custnum(custnum)
        self.set_mail(mail)

Customer
init中,您可能希望使用
super
而不是显式使用
Person
类。此外,在同一初始化中,您使用了
self.set_custnum
self.set_mail
作为变量,并将其定义为方法。尝试使用我编辑的
Customer
init

class Customer(Person):

    def __init__(self, name, address, telnum, custnum, mail):
        super().__init__(self, name, address, telnum)

        self.set_custnum(custnum)
        self.set_mail(mail)

目前,不要使用双下划线名称。如果您想将一个属性标记为private,只需一个
\u
就足够了,而且不会让您受到Python的属性名称争论的影响。非常感谢,这解决了我的问题。为什么在超类中我可以使用self.method=attribute而没有任何问题?我不知道。我得看看超级类的代码。
customer = Ch11Ex3Classes.Customer(name, address, telnum, custnum, mail)
print ('Customer Name: ', customer.get_name())
print ('Customer Address: ', customer.get_address())
print ('Customer telephone number: ', customer.get_telnum())
print ('Customer Number: ', customer.get_custnum())
print (customer.get_mail())
return self.__custnum
AttributeError: 'Customer' object has no attribute '_Customer__custnum'
class Customer(Person):

    def __init__(self, name, address, telnum, custnum, mail):
        Person.__init__(self, name, address, telnum)

        self.set_custnum(custnum)
        self.set_mail(mail)
class Customer(Person):

    def __init__(self, name, address, telnum, custnum, mail):
        super().__init__(self, name, address, telnum)

        self.set_custnum(custnum)
        self.set_mail(mail)