2010-07-12 55 views
0

嘿大家,如果你看過我以前的文章,你會知道我正在使用Python開發一個航空公司程序。Python 2.6:包含層次結構問題:相同的值:S

另一個困擾的問題是,在我啓動一個航班後,它會計算航班的持續時間並替換用於啓動航班的按鈕。但是當我購買另一架飛機時,它將兩個航班狀態都改變爲同一時間(狀態是持續時間 - 到達時間剩下時間,直到它再次降落)。

我的計劃是在這一點所以生病嘗試,並通過所有其他****篩選這是有相當大的:

在這裏你點擊「啓動飛行」

def Flights (GUI, Player): 
... 
    for AP in Player.Airplane_list: 
     GUI.la(text = AP.aircraft) 
     GUI.la(text = 'Flight Pax') 
     if AP.status == 0: 
      GUI.gr(2) 
      GUI.bu(text = 'Launch Flight', command = Callable(Launch_refresh, count, GUI, Player)) 
      GUI.bu(text = 'Edit Flight', command = Callable(flight_edit, GUI, count, Player)) 

頁Launch_refresh基本上刷新窗口,並啓動(下面)計算所有的時間和現金。而Self是玩家類,它將低於玩家類中發現的啓動方法。

def launch (self, Airplane_No): #Method used to calculate flight-time specific-data and set aircraft into flight 
    if self.fuel >= (self.Airplane_list[Airplane_No].duration*self.Airplane_list[Airplane_No].fuel_consump): 
     self.Airplane_list[Airplane_No].Flight.departure_time = datetime.now()#sets Departure Time to Now 
     self.Airplane_list[Airplane_No].Flight.arrival_time = self.Airplane_list[Airplane_No].Flight.departure_time+timedelta(self.Airplane_list[Airplane_No].duration)#Sets arrival Time 
     self.Airplane_list[Airplane_No].status = self.Airplane_list[Airplane_No].Flight.arrival_time-datetime.now()#Status to Arrival time minus now 
     self.fuel -= (self.Airplane_list[Airplane_No].duration*self.Airplane_list[Airplane_No].fuel_consump)#Subtracts Fuel Used 
     self.bank += self.Airplane_list[Airplane_No].Flight.income#Adds Flight Income to Bank 

這裏是球員類

class Player (object):#Player Class to define variables 
    '''Player class to define variables''' 

    def __init__ (self, stock = 0, bank = 1, fuel = 0, total_flights = 0, total_pax = 0, Airplane_list = Airplane([]), APValue_Total = 1): 
     ... 

內。然後Player.Airplane_list是擁有飛行類裏面他們飛機類的列表:

這裏是飛機類:

class Airplane (object):  
'''Airplane Class''' 

    def __init__ (self, company = '', aircraft = '', base_price = 0, flight_range = 0, pax = 0, 
        fuel_consump = 1, speed = 10, crew_pilots = 0, crew_cabin = 0, 
        crew_mechanics = 0, crew_cleaning = 0, staff_trg = 0, Total_price = 0, status = 0, Flight = Flight(departure_time = datetime(1,1,1), 
        distance = 2000, arrival_time = datetime(1,1,1)),duration = 1, airplane_details = []): 

並且您可以看到它具有Flight類,該類僅使用這3個參數(持續時間需要使用飛機的速度和飛行距離)

所以即時猜測,問題在於啓動方法內,但我不知道它到底在哪裏開始......然後它再次看起來很好:小號

+2

代碼格式看起來很可怕... – ChristopheD 2010-07-12 19:23:28

+0

而不僅僅是格式。這是一些醜陋的初始代碼雜貨清單。 – 2010-07-12 19:26:53

+0

大聲笑我知道,航空有很多數據可以使用。他們是由最大的init代碼呢。 – Pilot824 2010-07-12 23:04:47

回答

1

__init__碼將默認參數傳遞給一個對象:

class Airplane (object):  
'''Airplane Class''' 
    def __init__(self, ..., Flight = Flight(departure_time = datetime(1,1,1), ...): 

默認參數只計算一次,當類的定義,所以每一個飛機對象會得到相同的飛行,除非指定了它在構造函數參數中。我無法遵循你所問的所有問題,但這可能會導致你的問題。