【发布时间】:2017-01-01 06:25:53
【问题描述】:
我想子类化内置控件,例如axes;但是,据我所知,MATLAB 不允许以有记录的方式这样做。为了解决这个问题,我创建了一个名为 MyAxes 的类,如下所示。
MyAxes 有一个名为MATLABAxes 的已定义属性,它存储一个matlab.graphics.axis.Axes 对象。此坐标区对象是在构造时创建的。每个坐标区属性都会动态添加到正在构造的 MyAxes 对象中,从而创建包装器属性,这些属性应该简单地重定向到 MATLABAxes 属性。
每个包装器属性的get 方法都设置为MyAxes 的方法get_axes_property。此方法接受三个参数:
-
MyAxes对象本身 - 对轴控件的引用
- 属性名称
此方法效果很好,只是它为每个属性创建了一个新轴。我最终得到一个有 131 个子轴的图形!这似乎是因为在 MyAxes 构造函数中创建了轴。解决此问题的方法是要求首先创建 MATLAB 坐标区并将其作为参数传递给构造函数。这很不方便。
如何在MyAxes 构造函数中维护轴创建而不创建多个轴?当然,如果我偏离了轨道并且有更好的方法对内置控件进行子类化,我很想听听。
classdef Axes < handle & dynamicprops
properties
MATLABAxes;
end
methods
function obj = Axes
obj.MATLABAxes = axes;
axesPropertyList = properties( obj.MATLABAxes );
for property = axesPropertyList(:)'
propertyName = property{1};
obj.addprop( propertyName );
propertyInstance = obj.findprop( propertyName );
propertyInstance.GetMethod = @(x,y)obj.get_axes_property( obj.MATLABAxes, propertyName );
end
end
function value = get_axes_property( obj, control, propertyName )
value = control.(propertyName);
end
end
end
【问题讨论】:
标签: matlab oop inheritance