从超类

时间:2017-02-15 15:13:42

标签: matlab oop object subclass superclass

我有一个来自我自己的班级object的对象,它继承自我自己的班级data和我自己的班级graphic。为了表示对象object,它使用data - 超类中的属性和方法。现在我想用object - 超类中的方法绘制我的对象graphic,但是这个超类中的方法对它的object - 子类一无所知。

有没有办法将所需的数据(存储在object - 子类中)提供给graphic - 超类,而不将此信息放入像object.draw(this_data_stuff)这样的函数参数中来自draw - 超类的方法graphic和来自this_data_stuff - 子类的数据object

我想出的唯一方法是将子类对象object作为属性存储在graphic - 超类中,但将子类对象作为属性存储在超类对象中似乎很奇怪,而子类继承自同一个超类。

编辑:

数据级:

classdef data < handle

  properties
    data_stuff
  end

  methods

    function obj = data
    end

    function obj = compute(obj)
      obj.data_stuff = ... % math
    end

  end
end

图形级:

classdef graphic < handle

  properties
  end

  methods

    function obj = graphic
    end

    function obj = draw(obj)
      plot(obj.data_stuff,t) % ERROR, property 'data_stuff' is unknown
    end

  end
end

对象的类:

classdef object < data & graphic

  properties
  end

  methods

    function obj = object
    end

  end
end

测试

object_A = object;  % creates object
object_A.compute;   % generates data_stuff
object_A.draw;      % draws data_stuff, Error here, because 'draw` needs access to the 'data_stuff'-property.

0 个答案:

没有答案
相关问题