unexpected behavior with matlab.mix​in.Heterog​eneous

4 ビュー (過去 30 日間)
Scott Rowe
Scott Rowe 2014 年 11 月 3 日
編集済み: per isakson 2014 年 11 月 27 日
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

採用された回答

Geoff Hayes
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
  1 件のコメント
Scott Rowe
Scott Rowe 2014 年 11 月 3 日
Thanks Geoff,
Yup, that makes sense, although considering objects as pass by value is...different.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by