I have an abstract base class Parent, and two subclasses, Child1 and Child2. Both children implement a static method defined abstractly by the parent, myStaticTransform.
The goal is to create a static method, myStaticBuilder, in Parent which does some calculations, calls the appropriate myStaticTransform method, and then creates an instance of the appropriate child class with that data. Right now I have this defined as an instance method, as so.
% Parent.m
function child = myNotStaticBuilder(this)
% Do complicated math
x = 1 + 2;
% Transform the data (using this means the child class will dispatch its appropriate method
y = this.myStaticTransform(x);
% Get the child constructor the only way I know (it's gross and I hate it but shrug emoji)
childConstructor = str2func(class(this));
% Return the appropriate child object
child = childConstructor(y);
end
This is not appeasing, but it works, so I can live with it. As far as I've seen, this is how the internet thinks this should be done. Fine.
But ideally, I'd want to make this function static, since it does not rely on any object existing before it is called. If this was static, obviously I wouldn't be able to reference this (is it obvious I'm a Python programmer). Without that reference, I don't know how I would get access to the child constructor. As far as I can tell, I would also have to call Parent.myStaticTransform, which I think (but haven't verified) would throw an error because the parent implementation is abstract.
Is there a way to turn this into a static method, as I want, or should I just be thankful that Matlab has classes and not expect them do it right?
In Python, I would use a class method instead of a static method, which is a concept that Matlab does not have as far as I know.
@classmethod
def myClassBuilder(cls):
# Do complicated math
x = 1 + 2;
# Transform the data
y = cls.myStaticTransform(x)
# Return the appropriate child object
child = cls(y)
return child