子功能不会覆盖父功能

时间:2020-11-07 08:59:54

标签: python-3.x inheritance

我有一个关于继承的问题。现在,我不知道为什么我的子类__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

1 个答案:

答案 0 :(得分:0)

因此,经过反复试验和谷歌搜索,这是我的答案。请随时评论可以修改的内容!

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)
相关问题