【问题标题】:Python 2.6: Class inside a Class?Python 2.6:类中的类?
【发布时间】:2010-07-11 07:34:24
【问题描述】:

大家好,我的问题是我想弄清楚如何让一个班级进入另一个班级。

我正在做的是我有一个关于飞机的课程,其中包含关于它可以飞行多快、可以走多远、油耗等的所有统计数据。然后我有一个 Flight Class,其中包含有关航班的所有详细信息:距离、开始位置和时间、结束位置和时间、持续时间等等。

但我意识到每架飞机都有多个航班,那为什么不把所有的航班数据放到飞机类中呢?虽然我如何将一个类放到另一个类中,所以我可以这样调用:

Player1.Airplane5.Flight6.duration = 5 hours

我已经对飞机类做了一些处理,但是当我去保存信息(将所有内容都列出到文本文档中)时,它给我的只是数据的十六进制位置,而不是实际的字符串。

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

    def __init__ (self, stock = 0, bank = 200000, fuel = 0, total_flights = 0, total_pax = 0):
        self.stock = stock
        self.bank = bank
        self.fuel = fuel
        self.total_flights = total_flights
        self.total_pax = total_pax
        self.Airplanes = Airplanes
        self.flight_list = flight_list

有没有办法把一个类放在一个类中?还是我需要创建一个超级播放器类来处理我使用其他类的所有信息?

【问题讨论】:

    标签: python class


    【解决方案1】:

    我认为您混淆了对象和类。类中的类如下所示:

    class Foo(object):
        class Bar(object):
            pass
    
    >>> foo = Foo()
    >>> bar = Foo.Bar()
    

    但在我看来,这不是你想要的。也许您正在追求一个简单的包含层次结构:

    class Player(object):
        def __init__(self, ... airplanes ...) # airplanes is a list of Airplane objects
            ...
            self.airplanes = airplanes
            ...
    
    class Airplane(object):
        def __init__(self, ... flights ...) # flights is a list of Flight objects
            ...
            self.flights = flights
            ...
    
    class Flight(object):
        def __init__(self, ... duration ...)
            ...
            self.duration = duration
            ...
    

    然后你可以这样构建和使用对象:

    player = Player(...[
        Airplane(... [
            Flight(...duration=10...),
            Flight(...duration=15...),
            ] ... ),
        Airplane(...[
            Flight(...duration=20...),
            Flight(...duration=11...),
            Flight(...duration=25...),
            ]...),
        ])
    
    player.airplanes[5].flights[6].duration = 5
    

    【讨论】:

      【解决方案2】:

      听起来你在谈论聚合player 类的每个实例都可以包含零个或多个Airplane 实例,而这些实例又可以包含零个或多个Flight 实例。您可以在 Python 中使用内置的 list 类型来实现这一点,以节省用数字命名变量。

      class Flight(object):
      
          def __init__(self, duration):
              self.duration = duration
      
      
      class Airplane(object):
      
          def __init__(self):
              self.flights = []
      
          def add_flight(self, duration):
              self.flights.append(Flight(duration))
      
      
      class Player(object):
      
          def __init__ (self, stock = 0, bank = 200000, fuel = 0, total_pax = 0):
              self.stock = stock
              self.bank = bank
              self.fuel = fuel
              self.total_pax = total_pax
              self.airplanes = []
      
      
          def add_planes(self):
              self.airplanes.append(Airplane())
      
      
      
      if __name__ == '__main__':
          player = Player()
          player.add_planes()
          player.airplanes[0].add_flight(5)
      

      【讨论】:

        【解决方案3】:
        class Second:
            def __init__(self, data):
                self.data = data
        
        class First:
            def SecondClass(self, data):
                return Second(data)
        
        FirstClass = First()
        SecondClass = FirstClass.SecondClass('now you see me')
        print SecondClass.data
        

        【讨论】:

        • 需要一些功能描述。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-18
        • 2018-10-10
        相关资源
        最近更新 更多