フィルターのクリア

Change class property inside of class function/method

36 ビュー (過去 30 日間)
Metin Akyol
Metin Akyol 2022 年 2 月 7 日
コメント済み: Metin Akyol 2022 年 2 月 7 日
I would like assign a new value to a variable created in my class, but the below approach does not update the variable. Is there a different approach?
classdef class_1
properties
var_1
end
function consistency_check_trades(obj)
obj. var_1 = 2
end
end

採用された回答

Steven Lord
Steven Lord 2022 年 2 月 7 日
Your class does not inherit from the handle class so it has value class behavior. See this documentation page for a discussion of the differences between handle and values classes,
  3 件のコメント
Steven Lord
Steven Lord 2022 年 2 月 7 日
That's one way of solving the problem, yes. But being a handle class also comes with some other behaviors. If your class_1 objects aren't tied to something where it makes sense for multiple objects to refer to the same thing (like a figure window on screen):
f1 = figure; % f1 is a handle
f2 = f1; % f2 refers to the same figure as f1
f1.Color
ans = 1×3
1 1 1
f2.Color = 'r'; % Not changing f1's Color property
f1.Color % but the figure to which it refers changed so its property changed
ans = 1×3
1 0 0
Consider if leaving it as a value class (if that makes more sense) and returning the modified object from your method.
Metin Akyol
Metin Akyol 2022 年 2 月 7 日
Very helpful, thank you for adding some further explanation to this Steven.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by