2017-06-01 95 views
3
class Time: 

    def __init__(self,x,y,z): 
     self.hour=x 
     self.minute=y 
     self.second=z 

    def __str__(self): 
     return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second) 

    def time_to_int(time): 
     minutes=time.hour*60+time.minute 
     seconds=minutes*60+time.second 
     return seconds 

    def int_to_time(seconds): 
     time=Time() 
     minutes,time.second=divmod(seconds,60) 
     time.hour,time.minute=divmod(minutes,60) 
     return time 

    def add_time(t1,t2): 
     seconds=time_to_int(t1)+time_to_int(t2) 
     return int_to_time(seconds) 

start=Time(9,45,00) 
running=Time(1,35,00) 
done=add_time(start,running) 
print(done) 

我是新來的Python和我一直在做一些練習lately.I跨越一個問題來了,我已經寫了same.But我反覆得到代碼錯誤:「add_time未定義」。我試着定義一個main()方法,但它不打印任何東西。請幫助。調用裏面的方法類的Python

+1

請解決您的壓痕。 'time_to_int'等是否應該是獨立的函數或類的方法?如果是前者,他們需要在類定義之外進行縮進。 – BrenBarn

回答

3

您尚未創建上述類的對象。

類中的任何函數/方法只能由該類的對象訪問。有關面向對象編程基礎知識的更多信息,請參閱this頁面。

同時這個工作,定義類的方式如下:

class Time: 

def __init__(self,x=None,y=None,z=None): 
    self.hour=x 
    self.minute=y 
    self.second=z 

def __str__(self): 
    return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second) 

def time_to_int(time): 
    minutes=time.hour*60+time.minute 
    seconds=minutes*60+time.second 
    return seconds 

def int_to_time(seconds): 
    time=Time() 
    minutes,time.second=divmod(seconds,60) 
    time.hour,time.minute=divmod(minutes,60) 
    return time 

def add_time(t1,t2): 
    seconds=time_to_int(t1)+time_to_int(t2) 
    return int_to_time(seconds) 

和類塊之外,寫了下面幾行:

TimeObject = Time() 
start=Time(9,45,00) 
running=Time(1,35,00) 
TimeObject.add_time(start,running) 
print "done" 

不過我建議你寫的add_time函數在類之外,因爲您將對象傳遞給類作爲同一類中的函數的參數,並且在面向對象編程中被認爲是不好的設計。 希望它有幫助。乾杯!

+0

一個問題。現在根據你的代碼,所有的方法是獨立的嗎? – mathers25

+0

號碼開始和跑步是你課堂時間的對象。要訪問類Time的add_time方法,您需要另一個對象,因此我創建了一個TimeObject來訪問add_time方法。不過,我建議你使add_time方法獨立,並且可以在類中進行休息。 – crazyglasses

+0

在這裏你已經創建了一個TimeObject,但它必須被初始化,否則會給出一個錯誤。 – mathers25

-1

工作對我來說:

class C: 
    def f(a, b): 
    return a + b 
    x = f(1,2) 

print(C.x) 

,但你不應該做這樣的事情。通常你需要靜態方法或類方法(用@staticmethod@classmethod修飾)並在某些函數/實例化類中執行代碼時,類級別中的代碼正在執行。如果這是簡單的腳本,也可以在頂層(模塊)級別執行它。你的片段是「不好的練習」:類級別(我正在討論縮進)是用於聲明,而不是用於執行某些事情。在類級別上正常執行類似於C宏的代碼:例如,調用裝飾器,轉換某些方法/屬性/等 - 靜態的「純」函數!

1

這對我來說,你在你的構造函數

def int_to_time(seconds): 
    time=Time(0,0,0) # just set your 3 positionals args here 
    minutes,time.second=divmod(seconds,60) 
    time.hour,time.minute=divmod(minutes,60) 
    return time 

另一種方式來避免指定3個ARGS它可以正常工作,只要:

class Time: 
    def __init__(self,x=0,y=0,z=0): 
     self.hour=x 
     self.minute=y 
     self.second=z 

如果你想你的功能添加到您的類(例如time_to_int,int_to_time或甚至add_time),那麼您將需要縮進一個4個空格級別並將self添加到您的方法參數

+0

謝謝!我知道它。我試圖在不使用對象的情況下調用類方法。 – mathers25

1

斐伊川Mathers25,
我解決你的問題試試下面這個代碼,以獲得最佳的輸出,

class TimeClass: 

def __init__(self,x,y,z): 
    self.hour = x 
    self.minute = y 
    self.second = z 

def __str__(self): 
    return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second) 

def time_to_int(self,time): 

    minutes = (time.hour * 60) + time.minute 
    seconds = (minutes * 60) + time.second 
    return seconds 

def int_to_time(self,seconds): 
    time = TimeClass(0,0,0) 
     minutes,time.second=divmod(seconds,60) 
     time.hour,time.minute=divmod(minutes,60) 
     return time 

def add_time(self,t1,t2): 
    seconds = self.time_to_int(t1) + self.time_to_int(t2) 
    # Call method int_to_time() using self keyword. 
    return self.int_to_time(seconds) 


# First time object create that time set value is 0 of hour,minute and second 
TimeObject = TimeClass(0,0,0) 

# After create second object 
start=TimeClass(9,45,00) 

# After create thired Object 
running=TimeClass(1,35,00) 

# Store the value which return by add_time() 
done = TimeObject.add_time(start,running) 

# Display the value of done variable 
print(done)