unexpected behavior with matlab.mixin.Heterogeneous
4 次查看(过去 30 天)
显示 更早的评论
There seems to be an issue with inheritance when using
"matlab.mixin.Heterogeneous."
To illustrate consider two objects (code at bottom): Child, which inherits from Parent. The superclass method "changeParentVariable" is useless only when everything inherits from matlab.mixin.Heterogeneous. One would hope that root of inheritance (handle or matlab.mixin.Heterogeneous) would be immaterial. How can I make correct the behavior of matlab.mixin.Heterogeneous?
Behavior when everything inherits from "handle:"
>> test = Child(1,2)
test =
Child with properties:
childVariable: 2
parentVariable: 1
>> changeParentVariable(test,3)
>> test
test =
Child with properties:
childVariable: 2
parentVariable: 3
Behavior when everything inherits from "matlab.mixin.Heterogeneous:"
test = Child(1,2)
test =
Child with properties:
childVariable: 2
parentVariable: 1
>> changeParentVariable(test,3)
>> test
test =
Child with properties:
childVariable: 2
parentVariable: 1
...the superclass method was unable to modify the object when everything inherits from "matlab.mixin.Heterogeneous."
THE CODE
classdef Parent < handle % or matlab.mixin.Heterogeneous
properties
parentVariable = [];
end
methods
function obj = Parent()
obj.parentVariable = 0;
end
function changeParentVariable(obj,a)
obj.parentVariable = a;
end
end
end
classdef Child < Parent
properties
childVariable = [];
end
methods(Sealed)
function obj = Child(a,b)
obj = obj@Parent();
obj.parentVariable = a;
obj.childVariable = b;
end
end
end
0 个评论
采纳的回答
Geoff Hayes
2014-11-3
Scott - I think that you are encountering the different behaviours of using a handle class versus a value class. From comparing handle and value classes,
A value class constructor returns an instance that is associated with the variable to which it is assigned. If you reassign this variable, MATLAB® creates a copy of the original object. If you pass this variable to a function, the function must return the modified object.
A handle class constructor returns a handle object that is a reference to the object created. You can assign the handle object to multiple variables or pass it to functions without causing MATLAB to make a copy of the original object. A function that modifies a handle object passed as an input argument does not need to return the object.
Since matlab.mixin.Heterogeneous is a value class (see handle compatibility section of matlab.mixin.heterogeneous class) then you would have to modify your changeParentVariable to
function [obj] = changeParentVariable(obj,a)
obj.parentVariable = a;
end
and change your call to
test = test.changeParentVariable(3); % or test = changeParentVariable(test,3)
so that the parent variable is updated as
test =
child with properties:
childVariable: 2
parentVariable: 3
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!