How to call child static method from parent static method

3 ビュー (過去 30 日間)
Pavel Benes
Pavel Benes 2021 年 8 月 27 日
コメント済み: Walter Roberson 2022 年 5 月 19 日
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 日
One way:
function Greet(classname)
personName=feval(classname+".MyNameIs");
...
end
and then call as
AbstractPersonClass.Greet("George")
  5 件のコメント
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 件)

カテゴリ

Help Center および 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