Where does the `empty` method come from?

5 ビュー (過去 30 日間)
fa wu
fa wu 2023 年 8 月 20 日
コメント済み: fa wu 2023 年 8 月 24 日
The code come from this book 4.1.3
Then input mc=?SubClass in command line and open mc-->MethodList in workspace,you will find two method, one is 'SubClass' ,another is 'empty'.Where does the `empty` method come from? I don't built a `empty` methoed? Is it build-in method? I have doubts about this viewpoint.
Because you can input this in command line:
A=SubClass(1);
B=methods(A)
you will find there is just one method in object A.
Questions:
1 So Where does the `empty` method come from? And Why can't I find `empty` method using the `method` command in object A?"
2 The code obj=obj@Base(value); in SubClass.m. What does this code mean, and what is the special function of the '@' symbol?It doesn't appear to be a regular assignment statement.
classdef Base
properties(Access=private)
a;
end
methods
function obj=Base(value)
obj.a=value;
end
end
methods (Access=private)
function Fun(obj)
disp(num2str(obj.a));
end
end
end
classdef SubClass < Base
methods
function obj=SubClass(value)
obj=obj@Base(value);
end
end
end

採用された回答

Steven Lord
Steven Lord 2023 年 8 月 20 日
For question 1, as Bruno Luong wrote in this other question you asked,
'In this empty doc page one can read "empty is a hidden, public, static method of all nonabstract MATLAB® classes. You can override the empty method in class definitions."'
If you don't specifically define an empty method for your class, MATLAB will use a "default" Hidden (meaning it doesn't show up in the output of methods), public Access, Static (so you can call it without an instance of the class as input) method.
For question 2, that's how a subclass can call a superclass method (in this case the constructor of the superclass.) See this documentation page for more information about the limitations of that syntax.
  1 件のコメント
fa wu
fa wu 2023 年 8 月 24 日
Thanks for your anwser. It's very useful!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by