Where does the `empty` method come from?

조회 수: 7 (최근 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 CenterFile Exchange에서 Class Introspection and Metadata에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by