Class methods in "private" subfolder

16 Ansichten (letzte 30 Tage)
ErikJ GiesenLoo
ErikJ GiesenLoo am 3 Jan. 2023
Kommentiert: ErikJ GiesenLoo am 4 Jan. 2023
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 3 Jan. 2023
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 Kommentar
ErikJ GiesenLoo
ErikJ GiesenLoo am 4 Jan. 2023
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by