Constant property definition, does this example include a constructor?
Show older comments
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!
Accepted Answer
More Answers (0)
Categories
Find more on Subclass Definition in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!