How do I set a class property?
古いコメントを表示
Good afternoon! I'm bad at object-oriented and I need some advice. The situation is as follows: in one part of the code, I calculate the integer variable var. Then I want this variable to be declared as the default property in the class. For example,
var = a * b;
...
classdef vector
properties
v = var;
end
methods
%%
end
end
How can I implement this?
4 件のコメント
Walter Roberson
2021 年 1 月 26 日
If I recall correctly you need to declare var as a constant to be able to use it that way, and there are restrictions on how the constant can be created.
Are you trying to create a class variable, one that belongs to the class itself?
Igor Arkhandeev
2021 年 1 月 26 日
Mario Malic
2021 年 1 月 26 日
Hey Igor,
See this video on YouTube for the brief introduction on OOP:
Object-Oriented Programming in MATLAB | Master Class with Loren Shure
From my slim knowledge of OOP, you can write the constructor method to create a class with desired value for your variable.
Igor Arkhandeev
2021 年 1 月 26 日
採用された回答
その他の回答 (1 件)
Steven Lord
2021 年 1 月 26 日
0 投票
If you want to initialize the value once and have that be unchangeable for the rest of the lifetime of the object, make it a Constant property.
カテゴリ
ヘルプ センター および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!