Subclassing abstract property with defined class, e.g myprop@char, throws an error

1 回表示 (過去 30 日間)
Oleg Komarov
Oleg Komarov 2014 年 7 月 11 日
回答済み: Igor Varfolomeev 2016 年 3 月 29 日
Suppose I have a superclass that I want to use to create an interface:
classdef foo
properties(Abstract)
String@char
end
end
the property is set to char (I could also add vector, i.e. String@char vector , with same consequences).
Now, the concrete class that sets a default to that property is:
classdef subfoo < foo
properties
String = 'something';
end
end
However, in the command line:
>> subfoo
Error using subfoo
The property 'String' restriction defined in class 'subfoo' must match the property definition
in base class 'foo'.
I am forced to declare the property as String@char even in subfoo .
What's the rationale behind this?
PS. I am using an undocumented feature in OO of properties declaration, more details on http://undocumentedmatlab.com/blog/setting-class-property-types.

回答 (1 件)

Igor Varfolomeev
Igor Varfolomeev 2016 年 3 月 29 日
By the way, it's still the same in R2016a - even after this syntax feature was officially introduced.
Probably, they treat it like other qualifiers, like "Constant". From some points of view, it sounds reasonable, that you cannot define property as Constant, and then re-define it as non-Constant in an inherited class.
This limitation, most probably, is a result of the same built-in "do not re-define qualifiers" principle. And, personally, I find it much more inconvenient....

カテゴリ

Help Center および File ExchangeSubclass Definition についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by