python - VPython 继承

标签 python object inheritance vpython

我目前正在尝试创建一个类,其唯一目的是快速创建 VPython 对象并将附加值附加到该对象。 VPython 会自动创建一个具有位置和尺寸等值的对象。但是,我还想添加变量,例如 Material 的物理属性和动量。所以这是我的解决方案:

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 的文档。

最佳答案

我不使用 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)

但是,如果您知道要在原始 sphere 构造中声明的所有参数,我建议在您的子类中覆盖 __init__ 方法。

关于python - VPython 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15940665/

相关文章:

python - wsgi 文件不导入flaskapp

php - 使用 PDO 将对象条目插入数据库

javascript - 如何不通过整个响应映射对象

php - 在php中创建匿名对象

特定基类的 C++ 类模板

c++ - 函数返回基类而不是派生类,这是编码错误还是 Visual C++ 错误?

Python 的 Pandas 为 DatetimeIndex 提供了错误的工作日索引

python - os.walk 正则表达式

Python 文本文件行保持格式化

c++ - C++ 中是否有 'override' 说明符的反转?