- M and cr are value classes. See Comparison of Handle and Value Classes
- "I do not understand why I have to call the superclass"   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"   this is rather a description of a M has-a crack relationship
How to call subclass constructor
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    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
0 comentarios
Respuesta aceptada
  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"   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
 
Notes:
 
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
Más respuestas (0)
Ver también
Categorías
				Más información sobre Methods 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!

