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

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일
One way:
function Greet(classname)
personName=feval(classname+".MyNameIs");
...
end
and then call as
AbstractPersonClass.Greet("George")

댓글 수: 5

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.
You are completely right, this is something trivial... In every programming language except matlab
Thomas:
How would you do this in Prolog? Or Pascal? Or SNOBOL? Or Forth?
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
That link is to Object Pascal, not Pascal.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 8월 27일

댓글:

2022년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by