How to call child static method from parent static method
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pavel Benes
am 27 Aug. 2021
Kommentiert: Walter Roberson
am 19 Mai 2022
I have a parent class
classdef AbstractPersonClass
properties (Abstract, Access = public)
end
methods (Abstract, Access = protected, Static)
MyNameIs();
end
methods (Access = public, Static)
function [] = Greet()
personName = obj.MyNameIs();
fprintf(1, "Hi I am %s. \n", personName);
end
end
end
and a child class
classdef George < AbstractPersonClass
methods (Access = protected, Static)
function [name] = MyNameIs()
name = "George Smith";
end
end
end
You can run this by calling
George.Greet();
The method Greet in the parent class does not work because
personName = obj.MyNameIs();
is wrong, because I can't use obj in a static method.
I know that it would work if it the the method was not static, but I need it static.
Other possibility would be to make Greet abstract and implement it in each child class using child class name instead of the obj, but this method would be the same in each class, so I don't like this solution.
Is there any way to make Greet method work, while keeping it static and in the parent class?.
1 Kommentar
Catalytic
am 18 Mai 2022
I know that it would work if it the the method was not static, but I need it static.
I am very skeptical of this. Why would you absolutely need that? For that matter, why would you have a sub-class representing an individual person named "George"? The reason one defines a class is because you foresee many possible instances of that class being needed in your code. Do you really expect to have many George's running around in your application?
It makes much more sense to have a class organization where the name is a property of the class, like -
classdef PersonClass
properties
name
end
methods
function [] = Greet(obj)
disp("Hi, I am "+obj.name);
end
end
end
Then you do things like -
person=PersonClass("George");
person.Greet()
This way, you don't even need to define subclasses. If you only want certain people/names to be possible, you could make this an enumeration class -
classdef PersonClass
enumeration
George, Frank,Susan
end
methods
function Greet(obj)
disp("Hi, I am "+char(obj));
end
end
end
which would work very similarly -
person=PersonClass.George;
person.Greet()
Akzeptierte Antwort
Matt J
am 27 Aug. 2021
Bearbeitet: Matt J
am 27 Aug. 2021
One way:
function Greet(classname)
personName=feval(classname+".MyNameIs");
...
end
and then call as
AbstractPersonClass.Greet("George")
5 Kommentare
Thomas Michiels
am 19 Mai 2022
for pascal:
prolog is not an OOP oriented language, and thus has not have these concepts, which makes this an irrelevant question as you would not need it, as you are using different programming paradigms. (Matlab does have extensive OOP support, just with many little flaws)
same for SNOBOL, i see a pattern coming up, so let's update my statement:
In every programming language WHERE YOU CAN USE OOP except matlab
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!