How to call child static method from parent static method

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 件のコメント

Catalytic
Catalytic 2022 年 5 月 18 日
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()

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2021 年 8 月 27 日
編集済み: Matt J 2021 年 8 月 27 日

0 投票

One way:
function Greet(classname)
personName=feval(classname+".MyNameIs");
...
end
and then call as
AbstractPersonClass.Greet("George")

5 件のコメント

Pavel Benes
Pavel Benes 2021 年 8 月 27 日
Thank you for your answer, I will wait, if there will be some other answer, without feval, which I am trying to avoid and additional parameters for the Greet function. But this could be used if there won't be "nicer" solution. I have been trying to do somethign like this. I have been trying to get the name of the object from the method, so there would be no additional parameter for the Greet method. But it was returning AbstractPersonClass instead of George, so I have decided to ask on forums, that maybe it is something trivial that I don't know (I'm pretty new to OOP). Now I see that this is not something so trivial, if feval is needed.
I will wait some time and if nothing "better" appears I will accept this answer, thank you.
Thomas Michiels
Thomas Michiels 2022 年 5 月 18 日
You are completely right, this is something trivial... In every programming language except matlab
Walter Roberson
Walter Roberson 2022 年 5 月 18 日
Thomas:
How would you do this in Prolog? Or Pascal? Or SNOBOL? Or Forth?
Thomas Michiels
Thomas Michiels 2022 年 5 月 19 日
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
Walter Roberson
Walter Roberson 2022 年 5 月 19 日
That link is to Object Pascal, not Pascal.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by