Where does the `empty` method come from?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Steven Lord
le 20 Août 2023
'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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!