Interaction with super constructors and subclasses

I was not able to find help in the Matlab documentation and previous questions about using matlab inheritance and class constructors to create an interface. To make it neat, in the package.

Instead of dragging and dropping my code, I can condensate it like this:

The +MyPkg has a superclass Super and several subclasses Sub1 Sub2 ... Most of my properties and methods are defined in Super in such a way that Sub1 and Sub2 really exist only to use their constructors for simple procedures or, possibly, several methods overloaded from Super.

So, how do I start writing classes and constructors to support the interface, where I can use the following calls:

 a = MyPkg.Super(args).Sub1(args) b = MyPkg.Super(args).Sub1(args).Sub1Method 

In this case, I want to save arguments related to Super, in addition to arguments related to Sub1, for readability and organization.

Questions are welcome.

EDIT:

After reviewing the answer below and some views, I came to the conclusion that the interface shown above is actually not in the spirit of OO, and for my application of data analysis, a more suitable way to approach it would consist of a handle class with a constructor that fills the array objects or object property cells. Since the class is a descriptor class, it can then use the methods on it to obtain the desired methods. i.e. the following

 % in +MyPkg\ classdef Super < handle properties outputArray end methods function self = Super(args) self.outputArray=load_values(args); end function out = do_analysis(self,params) % do some analysis end end end 

Then, to use this:

 data1 = MyPkg.Super(args) % Populate the outputArray analysis1 = data1.do_analysis(params) 

and etc.,

Hope someone else handle these issues

+4
source share
1 answer

Regarding your question, you cannot if you use inheritance. Only direct superclass constructors can be called from subclasses, and only from a subclass can you call the superclass constructor. Link

Providing a superclass like this really breaks the basics of inheritance. Maybe you should think about another model, maybe about composition ("has" instead of "has"), if you need such access?

+2
source

Source: https://habr.com/ru/post/1392606/


All Articles