我有一个关于子类和父类之间继承的问题。目前,我的子类
__Stack__push()
中当前的 Stack3
函数无法覆盖父推送函数。据我所知,子类应该覆盖父类中定义的内容。然而,在这种情况下,它没有发生。
class Stack:
def __init__(self):
self.__list= []
def isEmpty(self):
return self.__list == []
def size(self):
return len(self.__list)
def clear(self):
self.__list.clear()
def push(self, item):
self.__list.append(item)
def pop(self): # popTail
if self.isEmpty():
return None
else:
return self.__list.pop()
def get(self):
if self.isEmpty():
return None
else:
return self.__list[-1]
def __str__(self):
output = '<'
for i in range( len(self.__list) ):
item = self.__list[i]
if i < len(self.__list)-1 :
output += f'{str(item)}, '
else:
output += f'{str(item)}'
output += '>'
return output
class Stack3(Stack):
def __init__(self,list):
super().__init__()
def push(self, item):
super().push(item)
self.__list = []
self.__list.insert(0,item)
def pop(self):
super().pop()
if self.isEmpty():
return None
else:
return self.__list.pop(0)
# main programme
s= Stack3(Stack())
print(s.pop())
for i in range(1,6):
s.push(i)
print('Content of stack =',s)
print('Item at top=',s.get())
print('Size=', s.size())
while not s.isEmpty():
print(s.pop())
print(s)
输出应该看起来像这样:
None
Content of stack = <5, 4, 3, 2, 1>
Item at top= 5
Size= 5
5
<4, 3, 2, 1>
4
<3, 2, 1>
3
<2, 1>
2
<1>
1
<>
但是我现在的样子是这样的
None
Content of stack = <1, 2, 3, 4, 5>
Item at top= 5
Size= 5
5
<1, 2, 3, 4>
此后它给了我这个错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-1-1eecf96bf9ca> in <module>
66 print('Size=', s.size())
67 while not s.isEmpty():
---> 68 print(s.pop())
69 print(s)
<ipython-input-1-1eecf96bf9ca> in pop(self)
53 return None
54 else:
---> 55 return self.__list.pop(0)
56
57 # main programme
IndexError: pop from empty list
所以,经过反复试验和谷歌搜索后,这是我的答案。有什么可以修改的地方欢迎留言!
class Stack:
def __init__(self):
self.__list= []
def isEmpty(self):
return self.__list == []
def size(self):
return len(self.__list)
def clear(self):
self.__list.clear()
def push(self, item):
self.__list.append(item)
def pop(self): # popTail
if self.isEmpty():
return None
else:
return self.__list.pop()
def get(self):
if self.isEmpty():
return None
else:
return self.__list[-1]
def __str__(self):
output = '<'
for i in range( len(self.__list) ):
item = self.__list[i]
if i < len(self.__list)-1 :
output += f'{str(item)}, '
else:
output += f'{str(item)}'
output += '>'
return output
class Stack3(Stack):
def __init__(self,list):
super().__init__()
self.__list = []
def push(self, item):
super().push(item)
self.__list.insert(0,item)
def isEmpty(self):
super().isEmpty()
return self.__list == []
def pop(self):
super().pop()
if self.isEmpty():
return None
else:
return self.__list.pop(0)
def __str__(self):
output = '<'
for i in range( len(self.__list) ):
item = self.__list[i]
if i < len(self.__list)-1 :
output += f'{str(item)}, '
else:
output += f'{str(item)}'
output += '>'
return output
# main programme
s= Stack3(Stack())
print(s.pop())
for i in range(1,6):
s.push(i)
print('Content of stack =',s)
print('Item at top=',s.get())
print('Size=', s.size())
while not s.isEmpty():
print(s.pop())
print(s)