AttributeError: 'Customer'对象没有属性'get_mailing_list
你好,我在运行下面的代码时遇到了一个错误:
AttributeError: 'Customer' 对象没有 'get_mailing_list' 这个属性。
我在代码里用了 sys 来调用父类,但不太确定怎么才能调用子类。
我还是个编程新手,所以我知道这可能是个“简单”的问题,但我就是想不明白。
提前谢谢你们的帮助。
# 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())
1 个回答
0
你把所有的方法都放在了 Customer
类的 __init__
里面,而不是直接放在这个类里。
class Customer(Person):
def __init__(self, ...):
...
def get_mailing_list(self): # <-- Notice the indentation level
return ...