Python 2.6:Class里面的类? [英] Python 2.6: Class inside a Class?

查看:161
本文介绍了Python 2.6:Class里面的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,大家,我的问题是,我想弄清楚如何得到一个类INSIDE另一个类。

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 statistics as to how fast it can fly, how far it can go, fuel consumption, and so on. Then I have a Flight Class which is all the details about the flight: Distance, starting location and time, ending location and time, duration, and so on.

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

But I realized that each airplane has multiple flights, so why not put all the flight data into the airplane class? Although how do i put a class INTO another class so i can call something like this:

Player1.Airplane5.Flight6.duration = 5 hours

Ive在飞机类方面做了一些工作,但当我去保存信息所有它都是数据的十六进制位置,而不是实际的字符串。

Ive somewhat done it with the airplane class, but when i go to save the information (listing everything out into a text document), all it gives me is the Hex location of the data and not the actual strings.

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

有一种方法可以把类放在类中吗?或者我需要创建一个超级Player类来处理所有使用其他类的信息。

Is there a way to put a class inside a class? or will i need to make one super Player class which handles all the information which im using other classes for?

推荐答案

是混乱的对象和类。类中的类如下所示:

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

这篇关于Python 2.6:Class里面的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆