Class methods in "private" subfolder

12 ビュー (過去 30 日間)
ErikJ GiesenLoo
ErikJ GiesenLoo 2023 年 1 月 3 日
コメント済み: ErikJ GiesenLoo 2023 年 1 月 4 日
The documentation on classes seems to suggest I can place private functions in a private folder:
But the explanation there is not clear enough to me. I kindof assumed if I created a folder named "private" within my "@class_name" folder that I could put function definitions corresponding to (Access = private) functions in there.
If anyone could clarify it would be greatly appreciated as my class currently has a lot of private and public methods and it might be good to organise them a bit.

採用された回答

Walter Roberson
Walter Roberson 2023 年 1 月 3 日
access = private for a property means "The property can be accessed only by members of the defining class." (and not by members of derived classes)
You can also designate access=private for methods, I think. https://www.mathworks.com/help/matlab/matlab_oop/specifying-methods-and-functions.html
The private folder is described at https://www.mathworks.com/help/matlab/matlab_prog/private-functions.html "The function is available only to functions and scripts in the folder immediately above the private subfolder."
Any functions defined in a private folder inside a class folder can only be called from the methods of the class. The functions have access to the private members of the class but are not themselves methods. They do not require an object to be passed as an input and can only be called using function notation. Use functions in private folders when you need helper functions that can be called from multiple methods of your class.
Thus, access=private has to do with defining properties and class methods that can only be accessed from direct class members, whereas the private folder can be used for helper functions that are not methods.
  1 件のコメント
ErikJ GiesenLoo
ErikJ GiesenLoo 2023 年 1 月 4 日
Thanks. I think that clarified it for me. I thihnk I missed the part where it said that the functions could not be class methods, so when I tried putting class methods inside a private folder it didn't work.
I think I have declared made some static functions, maybe I could turn some of those into regular functions and put them in private folder.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by