Python-访问在Class_B函数中创建和处理的Class_A对象

时间:2018-07-17 10:23:36

标签: python class object

我有两个类:Vehicle()和Route_plan()如下:

class Vehicle():
def __init__(self,unique_id):
    self.capacity = 9000
    self.unique_id = unique_id
    self.routes = []

class Plan():
    def assignment(self):
        # create a vehicle as 'this_vehicle' with id = 0 from class Vehicle()
        this_vehicle = Vehicle(0)
        this_vehicle.routes.append(5)
        this_vehicle.routes.append(8)

#run the route_plan process to assgin the specfic routes to vehicle 0
Plan().assignment()

output: [5,8]

现在,我还有另一个绘图功能可以尝试绘制每辆车的路线(在本例中为0):

def Draw():
    # if I have 10 vehicles (with id = 0,1,....9), how do I access them and their updated routes? 
    # Is there a simple way to update the vehicle's own variable (routes) directly using the vehicle ID and have it being flexibly called by any other classes and functions?
    # plotting process

2 个答案:

答案 0 :(得分:0)

让分配(自己)返回this_vehicle,然后在Draw()中使用Plan()。assignment()。

例如:

class Plan():
    def assignment(self):
        # create a vehicle as 'this_vehicle' with id = 0 from class Vehicle()
        this_vehicle = Vehicle(0)
        this_vehicle.routes.append(5)
        this_vehicle.routes.append(8)

        return this_vehicle

def Draw():
    vehicle0 = Plan().assignment()
    # whatever else you want to do

或者,将车辆存储在Plan类中。

class Plan():
    def assignment(self):
        # create a vehicle as 'this_vehicle' with id = 0 from class Vehicle()
        self.this_vehicle = Vehicle(0)
        self.this_vehicle.routes.append(5)
        self.this_vehicle.routes.append(8)

def Draw():
    route_plan = Plan()
    route_plan.assignment()
    # route_plan.this_vehicle now holds vehicle0
    # whatever else you want to do

多辆车: 为此,您必须重新计划一下,尽管我觉得这有点混乱,但这里最好是一本字典。

class Plan():
    def __init__(self):
        self.vehicles = dict()

    def assignment(self, id):
        # create a vehicle as 'this_vehicle' with id = 0 from class Vehicle()
        self.vehicles[id] = Vehicle(id)
        self.vehicles[id].routes.append(5)
        self.vehicles[id].routes.append(8)

def Draw():
    route_plan = Plan()
    route_plan.assignment(0)

    # route_plan.vehicles[0] now holds vehicle0
    # whatever else you want to do

答案 1 :(得分:0)

class Vehicle(object):
    def __init__(self,unique_id):
        self.capacity = 9000
        self.unique_id = unique_id
        self.routes = []

class Plan(object):

    @staticmethod
    def assignment():
        # create a vehicle as 'this_vehicle' with id = 0 from class Vehicle()
        this_vehicle = Vehicle(0)
        this_vehicle.routes.append(5)
        this_vehicle.routes.append(8)

        return this_vehicle
# This list will save all of objects created by vehicle class and you can
# change their attributes when ever you want 
vehicles_list = []
def Draw():
    vehicle = Plan.assignment()
    vehicles_list.append(vehicle)
    # now you have Your routes and you can complete Your plotting code
    # for example now you can change first vehicle routes  in this way:
    # vehicles_list[0].routes[0] = 6