Why is it forbidden to call a superclass method from a subclass function of a different a name?

2 ビュー (過去 30 日間)
I tried the thing in the screenshot below, i.e, to call the super-class method getThis() within the sub-class method someFunc(). It is forbidden, as the Code Analyzer message indicates. But why? Why should it matter that they are of the same name.

採用された回答

Amit Dhakite
Amit Dhakite 2023 年 2 月 14 日
Hi Matt,
In order to call a superclass method from a subclass's method, we must create a method in subclass with the same name as the method present in the super class.
Kindly go through the following document: Link
If you are using different name of the method, then you don't need to mention "@myclass" while calling the superclass method.
classdef mysubclass<myclass
methods
% This function has different name, so you don't have to mention
% @myclass here
function someFunc(obj)
obj.getThis();
end
% When we use same name, then we have to mention @myclass
function getThis(obj)
obj.getThis@myclass();
end
end
end
  2 件のコメント
Matt J
Matt J 2023 年 2 月 15 日
編集済み: Matt J 2023 年 2 月 16 日
Thanks, Amit. But I wonder why these restrictions should be necessary? Surely the syntax obj.getThis@myclass is pretty unambiguous as to where the desired definition of getThis() is to be found, regardless of where it is called from.
Jeff Miller
Jeff Miller 2023 年 2 月 15 日
Interesting question. Yes, the syntax seems unambiguous. Maybe the restriction simplifies implementing the OO framework, since it means that each subclass function only needs to keep track of at most one over-ridden function from the superclass, rather than having to potentially keep track of all of them.
Anyone know whether this would be allowed in other languages that support OO?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubclass Definition についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by