- what are your methods in the "Sub1" and "Sub2" classes? Do you have any of them sealed?
- What happens when you try with the following code
Sealed method inheritance in Heterogeneous arrays
조회 수: 22 (최근 30일)
이전 댓글 표시
I'm just starting to wrap my head around the "matlab.mixin.Heterogeneous" class. I understand why methods must be declared "Sealed" in order for me to call them on heterogeneous arrays, e.g. a base class with something like:
dbtype Base
classdef Base < matlab.mixin.Heterogeneous
methods (Sealed)
function tf = isValid(this)
...
end
end
end
allows me to create a heterogeneous array of two subclasses Sub1 and Sub2 and then call the isValid method on the array:
array = [Sub1(1) Sub2(NaN)];
isValid(array)
ans =
0 1
However, I don't see why I shouldn't be able to inherit functionality from further up the chain. Imagine I factor some of Base's functionality out into a parent class, Parent, giving me the class hierarchy:
matlab.mixin.Heterogeneous
|
Parent
|
Base
/ \
Sub1 Sub2
and Base now looks something like this:
dbtype
classdef Base < Parent
methods (Sealed)
function tf = isValid(this)
tf = isValid@Parent(this);
... yadda yadda
end
end
end
Base's isValid method is still Sealed, but it inherits some useful functionality from Parent's version of isValid. When my array is not heterogeneous, e.g.
array = [Sub1(1) Sub1(NaN)]
isValid(array)
ans =
1 0
then isValid works as expected. However, when I create a Heterogeneous array, MATLAB throws an exception:
array = [Sub1(1) Sub2(NaN)]
isValid(array)
Cannot call method 'isValid' because 'this' is heterogeneous and 'isValid' is not sealed. For more details please see the method dispatching rules for heterogeneous arrays.
The doc on method dispatching seems to say that I should be able to do what I'm trying to do ("The method Must be defined for the class of the heterogeneous array, either directly by the class of the array or inherited from a superclass."). Also, it seems obvious to me that this should work, since my heterogeneous array is now of type Base and Base inherits from Parent.
Could someone please help me either (1) figure out what I've got wrong, or (2) confirm that this should work but doesn't so I can put it in as a feature request? Thanks!
댓글 수: 1
Harsha Medikonda
2014년 8월 6일
%array=[Sub2(1) Sub2(NaN)]
%isValid(array)
and also the following code
%array=[Base(1) Base(NaN)]
%isValid(array)
답변 (1개)
Adam
2014년 8월 6일
편집: Adam
2014년 8월 6일
If I understand correctly, 'Parent' is your class that derives directly from Matlab.mixin.Heterogeneous.
That means that irrespective of what levels of inheritance there are further down the tree, if you create an array of objects from this tree and you wish to call a function on that array of objects that function must be sealed at 'Parent' level.
This isn't something I have tried in this particular case, but what I tend to prefer to do when I want derived classes to be able to provide additional functionality to a base class function is to have the base class be the only one that provides the isValid(...) function. Inside this it will provide any common functionality as befits a base class, but will then call a function called e.g. doIsValid(...). This will be a protected function of the base class (and empty in the base class), but the derived classes will over-ride this to add their specific functionality.
As I say I haven't tried this with Matlab.mixin.Heterogeneous so having a sealed function call a non-sealed function may still cause the same error (just as in e.g. C++ calling a non-const function from inside a const function would).
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Subclass Definition에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!