Given a class in OOP, shall I add the related methods signatures or not?

1 view (last 30 days)
I have a class BB, with properties and methods. It is defined in its own folder @BB. The methods are all defined in separate files .m within the folder. I have already read all the relative documentation.
Reading the documentation, it says, I should report in the BB class the methods signatures, as follows:
classdef BB < handle
properties
...
end
methods
function bb = BB( phy ) % Constructor
...
bb = set_resource_map( bb );
end
% the signatures of dynamic methods must be reported here
bb = set_resource_map( bb );
end
methods (Static)
% the signatures of static methods must be reported here
tags = tag_1D( indexes1d, tag_number );
end
end
Now the question is:
The class works also without reporting method signature as follows. Is it fine?
% the signatures of dynamic methods must be reported here
bb = set_resource_map( bb );
The Class does not work, instead, without reporting the static method signature, as follows. Is it fine?
% the signatures of static methods must be reported here
tags = tag_1D( indexes1d, tag_number );

Accepted Answer

Sameer
Sameer on 27 Mar 2024
Hi Marco
From my understanding, you want to know about the necessity of explicitly declaring method signatures within a MATLAB class definition, particularly noting a difference in behavior between ‘dynamic’ and ‘static’ methods.
In MATLAB, when you're working with classes defined within an @ directory (also known as a class folder), the methods of the class can be defined in two primary ways: within the classdef file itself or in separate files within the class directory. The behavior you're observing is related to how MATLAB treats these method definitions, especially in terms of ‘dynamic’ (instance) methods versus ‘static’ methods.
Dynamic Methods
For ‘dynamic’ methods (non-static methods that require an instance of the class to be called), MATLAB automatically recognizes methods that are defined in separate files within the @ directory, even if their signatures are not explicitly declared within the methods block of the class definition file. This is why your class works fine without explicitly declaring the signature of ‘set_resource_map’ within the class definition. MATLAB's class system is designed to be flexible in this regard, allowing for method definitions to be organized in separate files for better code organization and readability.
Static Methods
‘Static’ methods, on the other hand, behave differently. Since ‘static’ methods do not operate on an instance of the class but rather on the class itself, MATLAB requires an explicit declaration of these methods within the class definition file, typically within a methods (Static) block. This declaration is necessary for MATLAB to recognize and correctly route calls to these ‘static’ methods. Without the explicit declaration of static method signatures in the class definition file, MATLAB might not be able to find these methods, leading to errors when you try to call them.
I hope this helps!
Sameer

More Answers (0)

Categories

Find more on Software Development Tools in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!