With the exception of static methods, you must pass an object of the class explicitly to a MATLAB method?

13 views (last 30 days)
Method Syntax said:“With the exception of static methods, you must pass an object of the class explicitly to a MATLAB method.”
Static Methods said:"Therefore, you can call static methods without creating an object of the class."
There is a constructor MyClock.It is not a static Method. It didn't pass an object of the calsss,but it is working!
Why?
If I can call methods without creating an object of the class. Why do I need static methods?
The only value of static methods is to allow the creation of a method within a class that does not require input arguments?
classdef MyClock
properties
Time;
end
methods
function obj=MyClock()
obj.Time=datetime;
end
function val=get.Time(obj)
val=datetime;
end
end
end
It is weird,I delete get.Time input arguments 'obj' . I creat a MyClock object T: T=MyClock;
I find T.Time is non-existent. I set a breaking point in obj.Time=datetime;
I find the program did not execute get.Time block!
If I type:
T=MyClock;
T
It will call get.Time. T.Time is non-existent. It is make sense!
But the program did not execute get.Time block ,Why is T.Time non-existent?
classdef MyClock
properties
Time;
end
methods
function obj=MyClock()
obj.Time=datetime;
end
function val=get.Time() %I delete get.Time input arguments 'obj'
val=datetime;
end
end
end

Accepted Answer

Steven Lord
Steven Lord on 8 Aug 2023
You're looking at this documentation page? It could be a bit clearer, but you should interpret that statement in the context set by the first paragraph on the page: "This topic describes how to define class methods in MATLAB® using methods...end blocks, and it introduces method argument validation. This topic focuses on nonstatic, concrete methods, also referred to as ordinary methods. For other types of methods, see:" [Emphasis added.]
Neither Static methods nor the constructor are classified as an "ordinary methods".
Static methods do not require you to pass an instance of the class into the method. Neither does the constructor (the method with the same name as the class) though there are circumstances where it can be useful for the constructor to recognize when you've passed an instance of the class into the method. See the DocPolynom example on this documentation page and note that the plus() method automatically called the constructor on both inputs (to ensure they're both DocPolynom objects.) The constructor uses isa to check if the object is an instance of the DocPolynom class and if so uses the data from that instance to construct the new instance to be returned.
In that plus() on DocPolynom objects case, note that the method must receive at least one instance of that class as input but the instance need not be the first input. If P is a DocPolynom object, both P+1 and 1+P will call the DocPolynom plus() method, but 1+P will call it with the object as the second input. More importantly, both P-1 and 1-P will call the minus() method and in minus the order of the inputs matters.
  3 Comments
Steven Lord
Steven Lord on 8 Aug 2023
In summary, the documentation does not allow the definition of a constructor with no input arguments
Incorrect. In fact, there are some scenarios (listed in the "No Input Argument Constructor Requirement" section on this documentation page that you cited) when the constructor must be callable with no input arguments. And if you define a class that does not explicitly define a constructor, the default constructor that MATLAB gives you for free can only be called with no input arguments. But nothing prevents you from defining your own constructor and requiring users to call it with whatever data is needed to create the object.
even the set method does not allow the definition of a set method with no input arguments.
That's correct. Property accessor methods require exactly one (for get) or two (for set) input arguments, the first of which is always the object whose property you're trying to get or set. For set the second input argument is the new value you want to assign to the property.
If I had an array of Human objects and I somehow called the Human get.Name method with no input arguments, which Human's name would you expect that to return? Should every Human object just shout out its Name in a random order? [Note that MATLAB doesn't let you explicitly call the property accessor methods; it will call them when you try to access the property on an instance of that class.] No, I have to ask a specific Human what their Name property is and that specific Human is the required input to get.Name.
Now consider if the Human class had a method planetOfOrigin. [Yes, that would probably be better as a Constant property, but go with it for purposes of the exercise.] At least right now, planetOfOrigin wouldn't need information about a specific Human on which it was called since as far as we know no Human has been born on any planet other than Earth. So planetOfOrigin could be a Static method and be called "on the Human class as a whole" with the syntax Human.planetOfOrigin().
Let's look at your example.
classdef MyClock
properties
Time;
end
methods
function obj=MyClock()
obj.Time=datetime;
end
function val=get.Time(obj)
val=datetime;
end
end
end
The constructor, MyClock, accepts no inputs. That's fine. It will assign the current time (returned by the 0-input call to datetime) to the Time property of the MyClock object it's creating then returns that MyClock object.
If you wanted to give users the option of specifying their own time to set as the Time property of that object, you could modify the constructor. There are three main ways I'd allow to construct a MyClock object:
function obj=MyClock(timeToSet)
if nargin == 0 % If user called this with 0 inputs
obj.Time=datetime; % set Time property to current date & time
elseif isa(timeToSet, 'MyClock') % If user called this with a MyClock as input
obj = timeToSet; % just return the same MyClock
else % If user called this with some other input
obj.Time = timeToSet; % use that to set the Time property
end
end
Now looking at your get.Time property accessor, as written it doesn't access any information from the object that MATLAB passed into it. I'm guessing instead you want val to be the Time property from the object.
function val=get.Time(obj)
val=obj.Time;
end
Don't worry about the fact that we're accessing the Time property from within the Time property accessor. It doesn't get called recursively. In this case there's no real need to define a property accessor for the Time property, as it's not doing anything besides just returning the value stored in the object. If you don't define a property accessor that's what MATLAB does by default. [There's a bit of handwaving there, I'm ignoring overloading dot indexing. That's a much more involved topic.]
So I'd probably just define the class:
classdef MyClock
properties
Time;
end
methods
function obj=MyClock(timeToSet)
if nargin == 0 % If user called this with 0 inputs
obj.Time=datetime; % set Time property to current date & time
elseif isa(timeToSet, 'MyClock') % If user called this with a MyClock as input
obj = timeToSet; % just return the same MyClock
else % If user called this with some other input
obj.Time = timeToSet; % use that to set the Time property
end
end
% No need for get.Time, if C is a MyClock object get that property
% directly as "t = C.Time"
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Class File Organization in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!