为什么我的实例化函数不能创建'That'的“空白”实例?
我有以下最小类:
如果我运行:
并且一切都很好,我有一个类的“空白”实例。 如果我运行:
如您所见,我的实例化并没有起作用,我不知道为什么?它应该使用非空但是空白的实例替换空实例,不是吗?
更新:
SCFrench提供的链接指向http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html,在创建空数组部分有介绍,但这也没用:
我有以下最小类:
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
如您所见,我的实例化并没有起作用,我不知道为什么?它应该使用非空但是空白的实例替换空实例,不是吗?
更新:
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