2010-07-12 58 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), ...): 

默认参数只计算一次,当类的定义,所以每一个飞机对象会得到相同的飞行,除非指定了它在构造函数参数中。我无法遵循你所问的所有问题,但这可能会导致你的问题。