[英]Issue with Classes - Program works as intended, but visual studio code is highlighting syntax error
我的程序按預期工作,當我執行從賬戶 1 到賬戶 2 的轉賬時,它會輸出正確的值。
但是,Visual Studio 代碼在紅色下划線“self”(例如告訴我“'Account' 實例沒有'filepath' 成員)
你能幫我理解我做錯了什么嗎? 非常感謝!
這是我的代碼和下面的 output,非常感謝!
class Account:
def __init__(self):
filepath=self.filepath #self is underlined here
with open(filepath,"r") as file:
self.balance = int(file.read())
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def transfer(self, amount, receiver):
self.newbalance = self.balance - amount
receiver.newbalance = receiver.balance + amount
print(self.accountname,"balance is",self.balance) #self is underlined here
print(receiver.accountname,"balance is",receiver.balance)
print(self.accountname,"transfering",amount,"to",receiver.accountname) #self is underlined here
print(self.accountname, "new balance is",self.newbalance) #self is underlined here
print(receiver.accountname, "new balance is",receiver.newbalance)
class Louis(Account):
accountname = "Louis"
filepath = "louis.txt"
class Romain(Account):
accountname = "Romain"
filepath = "romain.txt"
romain = Romain()
louis = Louis()
romain.transfer(99,louis)
Output
Romain balance is 2000
Louis balance is 1000
Romain transfering 99 to Louis
Romain new balance is 1901
Louis new balance is 1099
問題是您只在子類中設置filepath
。 如果您嘗試創建一個Account
object 而不是Louis
或Romain
,或者您使用另一個未設置filepath
的子類,您將收到錯誤消息。 VS Code 無法知道您從不這樣做,它只是自行分析 class。
我相信 inheritance 在這里會導致一些混亂。 雖然 Louis 和 Roman 都初始化了filepath
,但賬戶基礎 class 從未初始化過filepath
。 Visual Studio 將此標記為語法錯誤,因為如果您已初始化 Account 的實例,您的代碼將引發AttributeError
。 例如:
class Account:
def __init__(self):
filepath=self.filepath #self is underlined here
with open(filepath,"r") as file:
self.balance = int(file.read())
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def transfer(self, amount, receiver):
self.newbalance = self.balance - amount
receiver.newbalance = receiver.balance + amount
print(self.accountname,"balance is",self.balance) #self is underlined here
print(receiver.accountname,"balance is",receiver.balance)
print(self.accountname,"transfering",amount,"to",receiver.accountname) #self is underlined here
print(self.accountname, "new balance is",self.newbalance) #self is underlined here
print(receiver.accountname, "new balance is",receiver.newbalance)
class Louis(Account):
accountname = "Louis"
filepath = "louis.txt"
class Romain(Account):
accountname = "Romain"
filepath = "romain.txt"
romain = Romain()
louis = Louis()
romain.transfer(99,louis)
account = Account()
Output
Romain balance is 2000
Louis balance is 1000
Romain transfering 99 to Louis
Romain new balance is 1901
Louis new balance is 1099
Traceback (most recent call last):
File "/home/test.py", line 38, in <module>
account = Account()
File "/home/test.py", line 4, in __init__
filepath=self.filepath #self is underlined here
AttributeError: 'Account' object has no attribute 'filepath'
您的文件路徑是 Class filepath
而不是實例變量
Louis 和 Romain 需要是Account
的實例
class Account:
def __init__(self, accountname, filepath):
self.filepath = filepath
self.accountname = accountname
self.balance = 0
with open(self.filepath,"r") as file:
self.balance = int(file.read())
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def transfer(self, amount, receiver):
self.newbalance = self.balance - amount
receiver.newbalance = receiver.balance + amount
print(self.accountname,"balance is",self.balance)
print(receiver.accountname,"balance is",receiver.balance)
print(self.accountname,"transfering",amount,"to",receiver.accountname)
print(self.accountname, "new balance is",self.newbalance)
print(receiver.accountname, "new balance is",receiver.newbalance)
romain = Account("Romain", "romain.txt")
louis = Account("Louis", "louis.txt")
romain.transfer(99,louis)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.