为什么我的 Instantiate 函数没有创建 That 的“空白”实例?
我有以下最小类:
classdef That < handle
properties
This = ''
end
methods
function Self = That(String)
if exist('String','var') == 1
Self.This = String;
end
end
function Self = Instantiate(Self)
if isempty(Self)
Self(1,1) = That;
end
end
end
end
如果我跑
This = That;
disp(size(This)) % 1x1
disp(isempty(This)) % False
一切都很好,我有一个“空白”类的实例
如果我跑
TheOther = That.empty;
disp(size(TheOther)) % 0x0
disp(isempty(TheOther)) % True
TheOther.Instantiate;
disp(size(TheOther)) % 0x0 - Expecting 1x1
disp(isempty(TheOther)) % True - Expecting False
如您所见,运行我的 Instantiate 不起作用,我不明白为什么?当然它应该用一个非空但空白的实例替换空实例?
更新 :
SCFrench 的链接指向创建空数组标题下的http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html,尽管这也不起作用:
function Self = Instantiate(Self)
if isempty(Self)
Blank = That;
Props = properties(Blank)
for idp = 1:length(Props)
Self(1,1).(Props{idp}) = Blank.(Props{idp});
end
end
end