此处SMS_Store
继承Message
,但每次调用父方法时都会出错:
NoneType object does not have attribute 'read_message'
。
并且存储在列表中的对象也返回None
值??
class Message():
def __init__(self, from_number, time_arrived, text_of_sms):
self.has_been_viewed=False
self.from_number= from_number
self.time_arrived = time_arrived
self.text_of_sms = text_of_sms
def getTime(self):
return self.time_arrived
def getText(self):
return self.text_of_sms
def setView(self):
self.has_been_viewed = True
def check_read(self):
return self.has_been_viewed
class SMS_Store(Message):
def __init__(self):
self.list_=[]
def add_new_arrivals(self,from_number, time_arrived, text_of_sms):
self.list_.append(Message.__init__(self,from_number,time_arrived,text_of_sms))
def message_count(self):
return len(self.list_)
def get_unread_index(self):
unread =[]
for i in range(len(self.list_)):
if(super(SMS_Store,self).check_read() == False):
unread.append(i)
return unread
def read_message(self,index):
self.list_[index].getText()
def delete(self,ints):
a = int(ints)
self.list_.pop(a)
def clear():
for i in list:
self.list_.pop(i)
inbox = SMS_Store()
inbox.add_new_arrivals(12,9,'some nights i stay up')
inbox.add_new_arrivals(12,5,'cashing in my bad luck')
inbox.add_new_arrivals(14,7,'somenights i call it a draw')
inbox.add_new_arrivals(56,0,'somenights i wish that my lips could built a castle.')
inbox.read_message(0)
答案 0 :(得分:0)
在这种情况下,您不应该使用继承。这是更正版本(也是固定代码样式):
class Message:
def __init__(self, from_number, time_arrived, text_of_sms):
self.has_been_viewed = False
self.from_number = from_number
self.time_arrived = time_arrived
self.text_of_sms = text_of_sms
def get_time(self):
return self.time_arrived
def get_text(self):
return self.text_of_sms
def set_view(self):
self.has_been_viewed = True
def check_read(self):
return self.has_been_viewed
class SMSStore:
def __init__(self):
self._list = []
def add_new_arrivals(self, from_number, time_arrived, text_of_sms):
self._list.append(Message(from_number, time_arrived, text_of_sms))
def message_count(self):
return len(self._list)
def get_unread_index(self):
return list(filter(lambda m: not m.check_read(), self._list))
def read_message(self, index):
return self._list[index].get_text()
def delete(self, index):
a = int(index)
self._list.pop(a)
def clear(self):
self._list = []
inbox = SMSStore()
inbox.add_new_arrivals(12, 9, 'some nights i stay up')
inbox.add_new_arrivals(12, 5, 'cashing in my bad luck')
inbox.add_new_arrivals(14, 7, 'somenights i call it a draw')
inbox.add_new_arrivals(56, 0, 'somenights i wish that my lips could built a castle.')
print(inbox.read_message(0))
答案 1 :(得分:0)
由于read_message()
仅由代码末尾的inbox.read_message(0)
语句调用,而不会产生错误,因此不清楚该错误的来源。(尽管read_message()
方法的返回值始终为None
,因为其中没有明确的return
语句。
无论如何,为了回答这个明确的问题,要明确地这样做,你会使用super()
方法 - 但这不是问题,SMS_Store
类不应该是' t是Message
的子类,因为它不是Message
类的专门化......而是{{em>容器 Message
1}}实例。
下面是代码的重写版本,它显示了如何分离这两个类,并获得大多数(如果不是所有)方法的工作。它现在也主要遵循PEP 8 - Style Guide for Python Code建议。
class Message:
def __init__(self, from_number, time_arrived, text_of_sms):
self.has_been_viewed = False
self.from_number = from_number
self.time_arrived = time_arrived
self.text_of_sms = text_of_sms
def get_time(self):
return self.time_arrived
def get_text(self):
return self.text_of_sms
def set_viewed(self):
self.has_been_viewed = True
def check_read(self):
return self.has_been_viewed
class SMS_Store:
def __init__(self):
self.list_ = []
def add_new_arrival(self, from_number, time_arrived, text_of_sms):
self.list_.append(Message(from_number, time_arrived, text_of_sms))
def message_count(self):
return len(self.list_)
def get_unread_index(self):
unread = []
for (i, message) in enumerate(self.list_):
if not message.check_read():
unread.append(i)
return unread
def read_message(self, index):
message_text = self.list_[index].get_text()
self.list_[index].set_viewed()
return message_text
def delete(self, index):
self.list_.pop(index)
def clear():
self.list_.clear()
inbox = SMS_Store()
inbox.add_new_arrival(12, 9, 'some nights i stay up')
inbox.add_new_arrival(12, 5, 'cashing in my bad luck')
inbox.add_new_arrival(14, 7, 'somenights i call it a draw')
inbox.add_new_arrival(56, 0, 'somenights i wish that my lips could built a castle.')
print(inbox.get_unread_index()) # -> [0, 1, 2, 3]
print(inbox.read_message(0)) # -> some nights i stay up
print(inbox.get_unread_index()) # -> [1, 2, 3]