Nested class to access other nested class' property

139 Views Asked by At

I'm looking for a MATLAB solution in which a nested subclass can access the properties of another nested subclass.

In the following example the superclass has two properties which each are initialised as two different subclasses:

classdef superclass

  properties
    prop1
    prop2
  end

  methods

    function obj = superclass()
       obj.prop1 = subclass1;
       obj.prop2 = subclass2;
    end

  end
end

The first subclass has the property a:

 classdef subclass1

    properties
      a
    end

 end

The second subclass has the property b and a method calcSomething which uses the property a of subclass 1:

 classdef subclass2

    properties
      b
    end

    methods
      function result = calcSomething(obj)
        result = obj.b * superclass.prop1.a;
      end
    end

 end

How can I express superclass.prop1.a to correctly fetch this property from within subclass2?

Thank you! :)

PS I'm not sure if my usage of the words superclass and subclass is entirely correct, since I didn't state

subclass < superclass

Maybe the concept of Mother and Children would be more convenient..?!

1

There are 1 best solutions below

0
On

Soo, following the main structure of superclass (which is not going to be altered) the method calcSomething will now be located inside superclass:

classdef superclass
   properties
      prop1 = subclass1
      prop2 = subclass2
   end

   methods
      function result = calcSomething(obj)
         result = obj.prop1.a * obj.prop2.b;
      end
   end
end