为什么我的实例化函数没有创建一个'空白'实例?MATLAB:将一个实例从一个空实例实例化到一个'空白'实例
我有以下最小类:
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不起作用,我不明白为什么?当然,它应该用一个非空的空白实例替换空实例吗?
UPDATE:
从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
你选择的变量名称真的是一些东西:) – Amro
这是一个关于这方面的游戏,以及其他的,厌倦了看到foobar无处不在:D – Carel
IMO具体例子总是更好;像'学生'类与'名称'属性 – Amro