Class methods in "private" subfolder
32 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
ErikJ GiesenLoo
le 3 Jan 2023
Commenté : ErikJ GiesenLoo
le 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.
Réponse acceptée
Walter Roberson
le 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."
The intersection of the two is described at https://www.mathworks.com/help/matlab/matlab_oop/organizing-classes-in-folders.html#br5wlnh
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!