簡體   English   中英

如何正確地從另一個文件繼承類

[英]How to correctly inherit class from another file

我有2個檔案。 driver.pystack.py 我想將基類stack.py的2個函數繼承到driver.py中 ,但實際上在創建類時遇到了問題(運行時遇到屬性錯誤)。 該代碼應要求用戶輸入,直到收到“ end”為止,並且程序應輸出所有輸入。

請不要理會格式錯誤,我在代碼片段中遇到了包括“導入”行的問題。

  • stack.py

 class Stack: def __init__(self): '''A new empty stack''' self.items = [] def push(self, o): '''Make o the new top item in this Stack.''' self.items.append(o) def pop(self): '''Remove and return the top item.''' return self.items.pop() def peek(self): '''Return the top item.''' return self.items[-1] def isEmpty(self): '''Return whether this stack is empty.''' return self.items == [] def size(self): '''Return the number of items in this stack.''' return len(self.items) class UpStack: def __init__(self): '''A new empty stack''' self.stack = [] def push(self, o): '''Make o the new top item in this Stack.''' self.stack.insert(0, o) def pop(self): '''Remove and return the top item.''' return self.stack.pop(0) def peek(self): '''Return the top item.''' return self.stack[0] def isEmpty(self): '''Return whether this stack is empty.''' return self.stack == [] def size(self): '''Return the number of items in this stack.''' return len(self.stack) 

  • driver.py

 from stack import * if __name__ == '__main__': #-----------------------------------Stack Code----------------------------------- s = Stack() s.push('Hello') print(s.pop()) data = "" while data.lower() != 'end': data = input("Enter Command: ") if data.lower() != 'end': s.push(data) while not s.isEmpty(): print(s.pop()) #-----------------------------------UpStack Code----------------------------------- u = UpStack() u.push('Hello') print(u.pop()) data = "" while data.lower() != 'end': data = input("Enter Command: ") if data.lower() != 'end': u.push(data) while not u.isEmpty(): print(u.pop()) 

  • driver.py嘗試的類版本

 from stack import * class Test: #-----------------------------------Stack Code----------------------------------- def stacker(self): s = Stack() s.push('Hello') print(s.pop()) data = "" while data.lower() != 'end' data = input("Enter Command: ") if data.lower() != 'end': s.push(data) while not s.is_empty(): print(s.pop()) #-----------------------------------UpStack Code----------------------------------- def upstacker(self): u = UpStack() u.push('Hello') print(u.pop()) data = "" while data.lower() != 'end': data = input("Enter Command: ") if data.lower() != 'end': u.push(data) while not u.is_empty(): print(u.pop()) 

由於我在實際運行代碼時什么都看不到,因此我為其創建了一個實例,這就是我所得到的。

    >>> s = Stack()
    >>> s
    <stack.Stack object at 0x103bb3a58>
    >>> s.push(2)
    >>> s.stacker()
    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in <module>
        s.stacker()
    AttributeError: 'Stack' object has no attribute 'stacker'

這是因為Stack()實例沒有stacker()方法。

stacker方法屬於您的Test類。

而不是打字

>>> s = Stack()
>>> s.stacker()

你應該使用

>>> t = Test()
>>> t.stacker()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM