Constant property definition, does this example include a constructor?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Xh Du
el 6 de Mzo. de 2017
Respondida: Steven Lord
el 7 de Mzo. de 2017
Hi all,
I'm very very new to OOP in MATLAB. When reading the documentation of constant property I have this question. The example gave is in this page: https://uk.mathworks.com/help/matlab/matlab_oop/properties-with-constant-values.html
Here is the code:
classdef MyProject < handle
properties (Constant)
ProjectInfo = MyProject
end
properties
Date
Department
ProjectNumber
end
methods (Access = private)
function obj = MyProject
obj.Date = datestr(clock);
obj.Department = 'Engineering';
obj.ProjectNumber = 'P29.367';
end
end
end
Question:
1. function obj = MyProject, is this a constructor? It doesn't look like a constructor as there is no properties input, if this is a constructor I'd expect something like this:
function obj = MyProject(Date, Department, ProjectNumber)
obj.Date = datestr(clock);
obj.Department = 'Engineering';
obj.ProjectNumber = 'P29.367';
end
2. In this case methods is set to 'private', meaning if I do a = MyProject, there would be an error. So how can I use this class apart from set access to public (like project1 = MyProject, but this gives an error)?
3. If I want to have constant and normal properties together in this class, how should I do it? Say add a non-constant property 'obj.square = projA ^ 2 + projB ^ 2', Shall I add projA and projB to properties, and define another function/constructor to do obj.square = projA ^ 2 + projB ^ 2?
Many thanks!
0 comentarios
Respuesta aceptada
Steven Lord
el 7 de Mzo. de 2017
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.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Numeric Types en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!