python如何继承父堆栈
嘿,伙计们,我是数据结构新手,我一直在想如何继承父类属性。 我试图将堆栈(父级)的属性继承到stack3(子级),但错误不断弹出:TypeError:init()缺少1个必需的位置参数:“\u stack3\u list” 任何帮助或建议都将不胜感激python如何继承父堆栈,python,class,Python,Class,嘿,伙计们,我是数据结构新手,我一直在想如何继承父类属性。 我试图将堆栈(父级)的属性继承到stack3(子级),但错误不断弹出:TypeError:init()缺少1个必需的位置参数:“\u stack3\u list” 任何帮助或建议都将不胜感激 class Stack: def __init__(self): self.__list= [] def isEmpty(self): return self.__list == [] def size(self):
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):
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):
self.__list = __list
super().__init__(self.__list)
def printMe(self):
super().__str__()
s = Stack3()
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)
类堆栈:
定义初始化(自):
self.\u list=[]
定义为空(self):
返回自我。列表==[]
def大小(自身):
返回len(self.\u列表)
def清除(自):
self.\u list.clear()
def推送(自身,项目):
self.\u list.append(项目)
def pop(自我):
如果self.isEmpty():
一无所获
其他:
返回self.\u list.pop()
def get(自我):
如果self.isEmpty():
一无所获
其他:
返回自我列表[-1]
定义(自我):
输出=“”
返回输出
第3类堆栈(堆栈):
定义初始化(自我,列表):
self.\u列表=\u列表
super()。\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
def printMe(自我):
超级()
s=Stack3()
打印(s.pop())
对于范围(1,6)内的i:
s、 推送(一)
打印('堆栈内容=',s)
打印('顶部的项=',s.get())
打印('Size=',s.Size())
而不是s.isEmpty():
打印(s.pop())
印刷品
如果要将所有参数从Stack3.\uuu init\uuu
传递到堆栈__初始化
使用:
class Stack3(Stack):
def __init__(self,*args, **kwargs):
super().__init__(*args, **kwargs)
有关*args
和**kwargs
用法的详细说明,请参阅