Hello everyone, i am new to the OOP in Matlab.
I have a superclass A
classdef A
properties
Property1
end
methods
function obj = A()
obj.Property1 = {};
end
function add(obj,inputArg)
obj.Property1{length(obj.Property1) + 1}=inputArg;
end
function str=output(obj)
str="";
for i=1:length(obj.Property1)
str=strcat(str," - ",string(obj.Property1{i}),newline);
end
end
end
end
and a subclass B
classdef B < A
methods
function obj = B()
obj.add('Start');
end
function outputArg = format(obj)
outputArg = strcat("List:",newline,obj.output());
end
end
end
Now I run the following test code:
x=B();
x.add('End');
fprintf(x.format());
The result is
List:
although I expected
List:
- Start
- End
. What have I done wrong?
Thanks a lot
Stephan

 採用された回答

Steven Lord
Steven Lord 2020 年 9 月 21 日

0 投票

As you've defined A and B they are value classes but you're using them like they are handle classes. See this documentation page and the "Object Modification" section on this other documentation page for more information.

1 件のコメント

Stephan
Stephan 2020 年 9 月 21 日
Thanks a lot!
classdef A < handle
Solve my problem!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeClass Introspection and Metadata についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 9 月 21 日

コメント済み:

2020 年 9 月 21 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by