【发布时间】:2017-06-07 04:08:34
【问题描述】:
我想创建一个抽象类的数组,并在数组的每个元素上调用一些 run() 方法,该方法引用该类的实现。
在 Java 之类的语言中(请原谅语法,这只是为了说明一点),代码类似于:
abstract class Base{
public void run();
}
class Derived0 extends Base{
public void run(){...};//overrides Base's run() method
}
class Derived1 extends Base{
public void run(){...};//overrides Base's run() method
}
class Derived2 extends Base{
public void run(){...};//overrides Base's run() method
}
主要:
Base baseArray[3];
baseArray[0] = new Derived0();
baseArray[1] = new Derived1();
baseArray[2] = new Derived2();
//runs each possible run() method, without main knowing any of them.
for(int i=0;i<3;++i){
baseArray.run();
}
我想在matlab中实现这个行为
到目前为止,我得到的是这个问题的公认答案:
Inheritence Polymorphism In Matlab
我缺少的是如何制作数组baseArray?
像baseArray = [] 这样的东西会导致baseArray(1) = Derived0; 期望翻倍,因此失败。
在matlab中不可能实例化Base的数组,因为类是抽象的,用matlab的创建对象数组的方式(我知道的)不能创建实例。
所以,请帮我在 Matlab 中实现一个多态数组。
我也很欣赏 python 解决方案,尽管 Matlab 解决方案是最好的。
谢谢!
【问题讨论】:
标签: python matlab oop polymorphism