1. Yes, this is the constructor for the class. The method of the class with the same name as the class is the constructor. Nothing says that the class constructor must accept input arguments. If the class author decided they knew when they implemented the class the exact values they wanted the properties to take on, they could certainly implement a 0-input constructor.
In the version of the example you posted, MATLAB Code Analyzer would (correctly) flag that the input arguments you specified in the definition of the constructor method are not used.
2. From that documentation page:
Because MyProject is a handle class, you can get the handle to the instance
that is assigned to the constant property:
p = MyProject.ProjectInfo;
This is an example of the singleton pattern. The Java example on that Wikipedia page has a getInstance method that is used to access the object stored in the INSTANCE property, but the MATLAB example on the documentation page allows direct access to the property ProjectInfo in which the object is stored. 3. You can certainly have Constant and non-Constant properties in the same class.
classdef car < handle
properties(Constant)
numberOfWheels = 4;
end
properties
color;
end
methods
function obj = car(thecolor)
obj.color = thecolor;
end
function describe(vehicle)
fprintf('The vehicle has %d wheels and is %s.\n', ...
vehicle.numberOfWheels, ...
vehicle.color);
end
end
end
Cars generally have 4 wheels; anything with a different number we would represent with an instance of a different class. Therefore we make that property Constant. But cars can come in many different colors, and it's not too difficult to change the color, so that needs to be a non-Constant property that can take on different values for different instances.
If we wanted we could even make a landvehicle base class with an Abstract and Constant numberOfWheels property and have the car, motorcycle, tractorTrailer, etc. subclasses each define their own value for their Constant numberOfWheels property (4 for the car subclass, 2 for the motorcycle, 18 for the tractorTrailer, 1 for the unicycle, 0 for the sled, etc.)
To address the last segment of the third part of your question, if you want the square property of your object to always take the current values of the projA and projB properties, you may want to make the square property Dependent. See the "Calculate Data on Demand" section of this documentation page for an example of Dependent properties.
Best Answer