How to call subclass constructor

2 visualizaciones (últimos 30 días)
Zoltán Csáti
Zoltán Csáti el 1 de Nov. de 2015
Comentada: Zoltán Csáti el 2 de Nov. de 2015
I have a class mesh1D. I want to create an another class, called crack to be the subclass of mesh1D. My subclass has one (apart from the inherited) property. What I would like to do is the following:
1) Create an instance of mesh1D, with e.g.
M = mesh1D([0 1], 10); M.generate;
2) Then create an object from the crack class:
cr = crack([0.13 0.75]);
The second one does not work. I read something, that perhaps the superclass constructor should be called, like
obj = obj@mesh1D(varargin);
But one thing I wanted to avoid with OOP is the long input argument lists the functions must provide. On the other hand, I do not understand why I have to call the superclass. I just want cr to be "part of" M, as if cr would be a structure within the M structure in functional programming.
These are my classes:
classdef mesh1D
%MESH1D Class for the 1D finite element mesh
% Detailed explanation goes here
properties
domain = [0 1];
nElem = 10;
nNode = 11;
node;
element;
connectElemNode;
end
methods
function obj = mesh1D(domain, nElem)
obj.domain = domain;
obj.nElem = nElem;
end
function obj = generate(obj)
a = obj.domain(1);
b = obj.domain(2);
gridSize = (b-a)/obj.nElem;
obj.node = [(1:obj.nElem+1)' (a:gridSize:b)'];
obj.element = [(1:obj.nElem)' (1:obj.nElem)' (2:obj.nElem+1)'];
end
end
end
classdef crack < mesh1D
%UNTITLED14 Summary of this class goes here
% Detailed explanation goes here
properties
place;
end
methods
function obj = crack(varargin)
obj.place = crackPlace;
end
end
end

Respuesta aceptada

per isakson
per isakson el 1 de Nov. de 2015
Editada: per isakson el 2 de Nov. de 2015
"I just want cr to be "part of" M" &nbsp I don't understand what you try to say. I read it as " M has a cr", but your code says " cr is-a M". Anyhow, try
M = mesh1D( [0 1], 10 );
M = M.generate;
cr = crack( [0 1], 10, [0.13,0.75] );
cr = cr.generate;
and
>> cr
cr =
crack with properties:
place: [0.1300 0.7500]
domain: [0 1]
nElem: 10
nNode: 11
node: [11x2 double]
element: [10x3 double]
connectElemNode: []
where
classdef mesh1D
properties
domain = [0 1];
nElem = 10;
nNode = 11;
node;
element;
connectElemNode;
end
methods
function obj = mesh1D(domain, nElem)
obj.domain = domain;
obj.nElem = nElem;
end
function obj = generate(obj)
a = obj.domain(1);
b = obj.domain(2);
gridSize = (b-a)/obj.nElem;
obj.node = [(1:obj.nElem+1)' (a:gridSize:b)'];
obj.element = [(1:obj.nElem)' (1:obj.nElem)' (2:obj.nElem+1)'];
end
end
end
and
classdef crack < mesh1D
properties
place
end
methods
function obj = crack(varargin)
obj = obj@mesh1D(varargin{1:2});
obj.place = varargin{3};
end
end
end
&nbsp
Notes:
  • M and cr are value classes. See Comparison of Handle and Value Classes
  • "I do not understand why I have to call the superclass" &nbsp because that's the way Matlab OOP is designed. The object cr doesn't know anything about the object M
  • "as if cr would be a structure within the M structure in functional programming" &nbsp this is rather a description of a M has-a crack relationship
&nbsp
has-a: try
cr = crack( [0.13,0.75] );
M = mesh1D( [0 1], 10, cr );
M = M.generate;
where
classdef mesh1D
properties
domain = [0 1];
nElem = 10;
nNode = 11;
node;
element;
connectElemNode;
crack
end
methods
function obj = mesh1D(domain, nElem, crk )
obj.domain = domain;
obj.nElem = nElem;
obj.crack = crk;
end
function obj = generate(obj)
a = obj.domain(1);
b = obj.domain(2);
gridSize = (b-a)/obj.nElem;
obj.node = [(1:obj.nElem+1)' (a:gridSize:b)'];
obj.element = [(1:obj.nElem)' (1:obj.nElem)' (2:obj.nElem+1)'];
end
end
end
and
classdef crack
properties
place
end
methods
function obj = crack(plc)
obj.place = plc;
end
end
end
  1 comentario
Zoltán Csáti
Zoltán Csáti el 2 de Nov. de 2015
Thank you for the detailed explanation, it helped me a lot. You are right, I wanted the has relationship. Somehow I thought that crack < mesh1D means that mesh1D has a crack. But as you explained it means that crack is a mesh1D.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by