Python-access类\在类\ B的函数中创建和处理的对象
我有两个类:Vehicle()和Route_plan(),如下所示:Python-access类\在类\ B的函数中创建和处理的对象,python,class,object,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
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):
让分配(自行)返回此车辆,然后在Draw()中使用Plan().assignment()
例如:
或者,将车辆存储在计划类别内
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
让分配(自行)返回此车辆,然后在Draw()中使用Plan().assignment()
例如:
或者,将车辆存储在计划类别内
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
目前,您没有将
Plan
实例分配给任何对象,也没有将Vehicle
存储在方法中的局部变量之外,因此:您无法访问它。在Draw()中调用Plan().assignment()
function您能给出一个将车辆存储在Plan类中的示例吗?@ThatBird即使在assignment
中,实际上也不会返回Vehicle
实例,因此在方法之外无法访问它。我建议您阅读Python中有关OOP的基本教程,例如,目前您没有将Plan
实例分配给任何对象,也没有将Vehicle
存储在方法中的局部变量之外,因此:您无法访问它。在Draw()中调用Plan().assignment()
function您能给出一个将车辆存储在Plan类中的示例吗?@ThatBird即使是assignment
也不会实际返回Vehicle
实例,因此它在方法之外是无法访问的。我建议阅读Python中关于OOP的基本教程,例如,如果我有10辆车辆(id=0,1,.9),我如何访问它们和它们更新的路线?更新的答案,虽然这有点混乱,因为您现在在两个不同的位置持有id,但没有其他逻辑方法可以很好地完成。不幸的是,它不起作用。报告“名称错误:未定义名称”self“。甚至当我为self.vehicles=dict()添加构造函数时,输出显示“无”我的坏,做了一件愚蠢的事情,这应该在init函数中创建(再次编辑答案)。在运行Draw()和print(Vehicle(0).routes)之后,如果我有10辆车(id=0,1,.9),我仍然会得到Vehicle 0的空列表,我如何访问它们和它们更新的路线?更新的答案,虽然这有点混乱,因为您现在在两个不同的位置持有id,但没有其他逻辑方法可以很好地完成。不幸的是,它不起作用。报告“名称错误:未定义名称”self“。即使在我为self.vehicles=dict()添加构造函数时,输出也会显示“无”。我的坏,做了一件蠢事,应该在init函数中创建(再次编辑答案)。运行Draw()和print(Vehicle(0.routes)之后,我仍然得到车辆0的空列表。例如,如果您有10辆车辆,当它们通过赋值函数返回时,您必须将它们保存在不同的变量中,如果您没有将它们赋值给任何变量,则无法使用和操作,那么您的意思是在类计划()中创建新变量来存储每个唯一的车辆?(这对我来说似乎是重复的工作)有没有一种简单的方法来更新车辆自身的变量(路由)并让它被任何其他类和函数调用?没有,我的意思是在代码主体中声明一些东西,比如列表(类和函数之外)然后在那里附加所有车辆对象,然后在需要时对其进行操作。请参阅我的更新答案这是我以前做过的,但我不喜欢这种方法。无法理解为什么我们无法在对象创建后(无论在何处创建和更新)轻松从对象中提取属性好的,也许你需要学习面向对象编程,看起来你对它还不够熟悉,幸运的是!如果你有10辆车,当它们通过赋值函数返回时,你必须将它们保存在不同的变量中,如果你没有将它们赋给任何变量,那么就不可能使用和操作它们,你的意思是创建新的va类计划()中的变量来存储每个唯一的车辆?(这对我来说似乎是重复工作)有没有一种简单的方法来更新车辆自己的变量(路由)并让它被任何其他类和函数调用?没有,我的意思是在代码的主体中声明一些类似列表的东西(类和函数之外)然后在那里附加所有车辆对象,然后在需要时对其进行操作。请参阅我的更新答案这是我以前做过的,但我不喜欢这种方法。无法理解为什么我们无法在对象创建后(无论在何处创建和更新)轻松从对象中提取属性好吧,也许你需要学习面向对象编程,看起来你对它还不够熟悉,幸运吧!
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