Python 2.6: Class inside a Class?

前端 未结 3 1735
小蘑菇
小蘑菇 2020-12-22 20:16

Hey everyone, my problem is that im trying to figure out how to get a class INSIDE another class.

What I am doing is I have a class for an Airplane with all its stat

相关标签:
3条回答
  • 2020-12-22 20:49

    It sounds like you are talking about aggregation. Each instance of your player class can contain zero or more instances of Airplane, which, in turn, can contain zero or more instances of Flight. You can implement this in Python using the built-in list type to save you naming variables with numbers.

    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)
    
    0 讨论(0)
  • 2020-12-22 20:58
    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
    
    0 讨论(0)
  • 2020-12-22 21:01

    I think you are confusing objects and classes. A class inside a class looks like this:

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

    But it doesn't look to me like that's what you want. Perhaps you are after a simple containment hierarchy:

    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
            ...
    

    Then you can build and use the objects thus:

    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
    
    0 讨论(0)
提交回复
热议问题