如何在同一個文件中調用一個類。我的文件是這樣的:Python:如何在同一個文件中調用一個類
class one:
def get(self):
return 1
class two:
def init(self):
get class one get()
如何獲得一個類get()。對不起,我的英語不好。
如何在同一個文件中調用一個類。我的文件是這樣的:Python:如何在同一個文件中調用一個類
class one:
def get(self):
return 1
class two:
def init(self):
get class one get()
如何獲得一個類get()。對不起,我的英語不好。
您可以撥打One.get()
直接,如果你把它變成了一個靜態方法:
class One:
@staticmethod
def get():
return 1
class Two:
def __init__(self):
val = One.get()
沒有@staticmethod
,你需要的One
一個實例,以便能夠調用get()
:
class One:
def get(self):
return 1
class Two:
def __init__(self):
one = One()
val = one.get()
您確定您需要在此處有課嗎?或者一個簡單的功能就足夠了? – uselpa 2013-03-03 08:40:07
是的,我需要它來防止功能混淆,以及更容易記住。 – Natsume 2013-03-03 09:38:48
你是什麼意思「防止功能混淆」?根據你的描述,我不認爲你真的明白哪些類是**。 – 2013-03-03 12:37:32