Several questions about overload the 'copyElement' method in subclasses of 'matlab.mixin.Copyable'
6 views (last 30 days)
Show older comments
1、The copy method makes a shallow copy of the object
2、copyElement is a Protected Methods
3、MATLAB® does not call copy recursively on any handles contained in property values.
4、The copy method copies data without calling the class constructor or property set functions. It therefore produces no side effects.
There is four questions:
1、The copy method makes a shallow copy of the object.--------But in overload copyElement code use ’copy‘ to Make a deep copy of the DeepCp object ---this page Why?So, can the copy method perform deep copying ? Or not?
methods(Access = protected)
% Override copyElement method:
function cpObj = copyElement(obj)
% Make a shallow copy of all four properties
cpObj = copyElement@matlab.mixin.Copyable(obj);
% Make a deep copy of the DeepCp object
cpObj.DeepObj = copy(obj.DeepObj);
end
end
By running the following code, you will notice that as long as it's an object of the matlab.mixin.Copyable class, using copy will achieve deep copying. Therefore, is it not an incorrect conclusion to say, "The copy method makes a shallow copy of the object"?
classdef DeepCp < matlab.mixin.Copyable
properties
DpProp
end
methods
function obj = DeepCp(val)
obj.DpProp=val;
end
end
end
A=DeepCp(7);
B=copy(A);
A.DpProp=222;
B
2、copyElement is a Protected Methods ,--------So, is it necessary to declare Access = protected when overloading copyElement? Is it always required to do so? Do you have to redeclare all method attributes when overloading built-in methods in MATLAB?
3、MATLAB® does not call copy recursively on any handles contained in property values.--------Since the copy method is sealed, it cannot be overloaded or defined. So how can a situation with recursive calls to copy occur? For instance, in the case of get methods, when you define a get method within a class, it may reference the class's properties, creating a possibility of recursive calls to the get method. Therefore, it is explicitly stated that there should not be a recursive call situation in get methods.
4、The copy method copies data without calling the class constructor or property set functions. It therefore produces no side effects.---------What built-in methods call "the class constructor or property set functions"? What are the side effects of calling the class constructor or property set functions?
0 Comments
Answers (0)
See Also
Categories
Find more on Handle Classes in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!