Where does the `empty` method come from?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
fa wu
el 20 de Ag. de 2023
Comentada: fa wu
el 24 de Ag. de 2023
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 comentarios
Respuesta aceptada
Steven Lord
el 20 de Ag. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!