首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:'Customer‘对象没有属性'get_mailing_list’

AttributeError:'Customer‘对象没有属性'get_mailing_list’
EN

Stack Overflow用户
提问于 2014-07-04 15:00:39
回答 1查看 954关注 0票数 0

你好,我收到了以下错误: AttributeError:'Customer‘object在运行下面的代码时没有属性'get_mailing_list’,这里有sys来调用超类,但不知道如何使它调用子类。再次,我是一个编程新手,所以我相信这是一个“简单”的修复,但对于我的生活,我看不到。提前谢谢。

代码语言:javascript
复制
# This Person class holds general data about people and will
# end up as the superclass for this example.
class Person:
    #__init__ method initialzes the attributes.
    def __init__(self, name, address, phone):
        self.__name = name
        self.__address = address
        self.__phone = phone

    # The set_name method gets the persons name.
    def set_name(self, address):
        self.__name = name

    # The set_address method gets the persons address.
    def set_address(self, address):
        self.__address = address

    # The set_phone method gets the persons phone number.
    def set_phone(self, phone):
        self.__phone = phone

    # The get_name method returns the name.
    def get_name(self):
        return self.__name

    # The get_address method returns the address.
    def get_address(self):
        return self.__address

    # The get_phone method returns the phone number.
    def get_phone(self):
        return self.__phone

# The Customer class holds the general data from superclass Person
# as well as customer number and mailing list info making it a subclass
# of Person.
class Customer(Person):
    # __init__ method initializes the attributes.
    def __init__(self, name, address, phone, customer_number, mailing_list):
        # Call the superclass
        Person.__init__(self, name, address, phone)
        self.__customer_number = customer_number
        self.__mailing_list = mailing_list

        # The set_customer_number method get customer number.
        def set_customer_number(self, customer_number):
            self.__customer_number = customer_number

        # The set_mailing_list method gets yes or no to mailing list.
        def set_mailing_list(self, mailing_list):
            self.__mailing_list = mailing_list

        # The get_customer_number method returns the customer number.
        #def get_customer_number(self):
           # return self.__customer_number

        # The get_mailing_list method returns the yes or no response.
        def get_mailing_list(self):
            return self.__mailing_list

# This program will test the Person superclass and Customer subclass while
# by returning and displaying the gathered information.

import sys

# Get the cutomer info.
name = input('Name: ')
address = input('Address: ')
phone = input('Phone: ')
customer_number = input('Customer number: ')
mail = input('Include in mailing list? (y/n): ')

# Determine True or False for mailing list.
if mail.lower() == 'y':
    mailing_list = True
else:
    mailing_list - False

# Create an instance of the Customer class.
my_customer = Customer(name, address, phone, customer_number, mailing_list)

# Display the object's data.
print('Customer Information')
print('---------------------')
print('Name:', my_customer.get_name())
print('Address:', my_customer.get_address())
print('Phone:', my_customer.get_phone())
#print('Customer number:', my_customer.get_customer_number())
print('Mailing list:', my_customer.get_mailing_list())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-04 15:04:15

您是在Customer上定义__init__中的所有方法,而不是在类上。

代码语言:javascript
复制
 class Customer(Person):
     def __init__(self, ...):
         ...

     def get_mailing_list(self):  # <-- Notice the indentation level
         return ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24576983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档