Azzera filtri
Azzera filtri

Calling subclass functions to manipulate inputs for superclass constructor?

28 visualizzazioni (ultimi 30 giorni)
I have a user-created subclass Domain2d < fegeometry. The fegeometry class is a default class in MATLAB, and the inputs for the fegeometry constructor are not user-friendly. Part of the idea of the Domain2d subclass is that its constructor should take user-friendly inputs, translate them into the complex inputs needed for the fegeometry constructor, then call the fegeometry constructor. The way the code is currently written, the task of manipulating the simple inputs into complex inputs is like:
classdef Domain2d < fegeometry
properties
end
methods
function self = Domain2d(simple_inputs)
% manipulate simple_inputs to complex_inputs
...(code here)
% call superclass constructor
self@fegeometry(complex_inputs);
end
end
end
This is kludge-y, but it works, and substantial code has been written that uses the bar object. But now I have a good reason to want to rewrite the bar object like
classdef Domain2d < fegeometry
properties
end
methods
function self = Domain2d(simple_inputs)
complex_inputs = translateSimpleToComplex(self,simple_inputs);
self@fegeometry(complex_inputs);
end
function complex_inputs = translateSimpleToComplex(self,simple_inputs)
...
end
end
end
The above produces the error "Calling the superclass constructor 'fegeometry' after an object use or after a return statement is not supported."
How should I proceed?
  2 Commenti
Matt J
Matt J il 27 Giu 2024 alle 20:50
Modificato: Matt J il 27 Giu 2024 alle 20:51
The handle class is a default class in MATLAB, and the inputs for the handle constructor are not user-friendly
Does the handle class constructor even accept inputs? What documentation are you getting this from? I've never heard of anyone explicitly calling the handle superclass constructor.
Tyler Fara
Tyler Fara il 27 Giu 2024 alle 21:07
Modificato: Tyler Fara il 27 Giu 2024 alle 21:31
Sorry, my dummy names were maybe poorly chosen. I've seen people use the terms "handle" and "bar" to refer to a superclass and a subclass; I guess because a bar is a part of a handle? But in MATLAB, a handle is also its own class right? Anyway, what I meant by "handle" and "bar" were two dummy classes. The actual class names are Domain2d < fegeometry and I've edited my question to reflect these names.

Accedi per commentare.

Risposta accettata

Steven Lord
Steven Lord il 27 Giu 2024 alle 20:54
I have a user-created subclass bar < handle.
Don't call your class bar. It already has a meaning in MATLAB.
Looking at the body of your constructor:
function self = bar(simple_inputs)
complex_inputs = translateSimpleToComplex(self,simple_inputs);
self@handle(complex_inputs);
end
the error message is correct. You cannot use self as an input to the translateSimpleToComplex function and then afterwards try to call the superclass constructor to create/initialize the variable self.
Why are you passing self as an input into translateSimpleToComplex? Is it simply so you can call the method and you're not actually trying to call methods or access properties of your class? If so turn it from an instance method (one that requires an instance of the class as input) into a Static method and call it using the name of your class, mybar.translateSimpleToComplex(simple_inputs) if you rename your class to mybar as an example.
  3 Commenti
Tyler Fara
Tyler Fara il 27 Giu 2024 alle 21:37
OK TL;DR, your suggestion about static methods should work. Thank you so much!! You know when you pick up a new skill or tool and you just know in advance that it's going to be super powerful? That's this moment right now for me, and that's thanks to you. I appreciate it!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by