我目前正在尝试创建一个类,唯一的目的是快速创建一个VPython对象并将附加值附加到该对象。VPython会自动创建具有位置和尺寸等值的对象。但是,我也想添加一些变量,比如材料的物理属性和动量。所以这是我的解决方案:
class Bsphere(physicsobject):
def build(self):
sphere(pos=ObjPosition, radius=Rad,color=color.red)其中的物理对象看起来像这样:
class physicsobject:
def __init__(self):
self.momentum=Momentum本质上,我希望它在添加新变量时仍然保留VPython sphere()对象的原始属性。这实际上在一开始就起作用了,对象呈现,变量被添加。但是现在,我没有办法更改VPython对象。如果我键入:
Sphereobj.pos=(1,2,3)该位置将作为变量更新,但是,VPython不会更新渲染对象。现在,对象和渲染对象之间会断开连接。有没有办法在创建新对象时继承VPython对象的渲染特性?我不能简单地使用
class Bsphere(sphere(pos=ObjPosition, radius=Rad,color=color.red)):
self.momentum=Momentum而且关于VPython的文档也不多。
发布于 2013-04-11 14:29:14
我不使用VPython。但是,从外观上看,您继承的是physicsobject而不是sphere的属性。我的建议是试试这个:
# Inherit from sphere instead
class Bsphere(sphere):
# If you want to inherit init, don't overwrite init here
# Hence, you can create by using
# Bpshere(pos=ObjPosition, radius=Rad,color=color.red)
def build(self, material, momentum):
self.momentum = momentum
self.material = material然后,您可以使用:
myobj = Bsphere(pos=(0,0,0), radius=Rad,color=color.red)
myobj.pos(1,2,3)但是,我建议在子类中使用overwrite __init__方法,前提是您知道要在原始sphere构造中声明的所有参数。
发布于 2015-05-07 19:46:21
from visual import *
class Physikobject(sphere):
def __init__(self):
sphere.__init__(self, pos = (0,0,0), color=(1,1,1))
self.otherProperties = 0我认为这一点很有帮助--这个问题可能已经过时了,因为人们可能还在思考它。
发布于 2015-09-13 08:00:14
我是一个vpython的大用户,我从来没有用过这样的东西,但我知道vpython已经有了你想要实现的特性。
===============================Example====================================
from visual import *
myball = sphere()
myball.weight = 50
print (myball.weight)这段代码创建一个球,然后初始化一个名为weight的变量,然后显示它。
https://stackoverflow.com/questions/15940665
复制相似问题