フィルターのクリア

Immutable properties and subclass constructors

8 ビュー (過去 30 日間)
Christian Veenstra
Christian Veenstra 2017 年 8 月 8 日
Is it possible to somehow set immutable superclass properties in a subclass constructor? It seems not, but maybe I'm just misinterpreting the documentation and error messages and this is possible somehow?
In case this doesn't make sense, my motivation is this: I've written a fairly nice data-analysis object that I use as a superclass all the time. It loads data from the data acquisition tool we normally use, and I have different subclasses for different experiments, which have different analysis requirements but most of the basics are the same (and located in the superclass).
Now I'd like to re-use all that code by loading data (of essentially the same form) but from a different filetype (new constructor required) into the properties expected by the superclass. I could just make the properties protected in the superclass (instead of immutable), but I'd really like them to only be changed within the constructor of the new subclass.
Or maybe I should be approaching this in a different way entirely?

採用された回答

Matt J
Matt J 2017 年 8 月 9 日
編集済み: Matt J 2017 年 8 月 9 日
Thank you, but I should have rephrased my question - I was looking for a way to overwrite the value set by the superclass constructor.
I'm not sure why it would be preferable to overwrite when you can just set the value in the subclass constructor directly. Nevertheless, if you must do this, an alternative would be to convert your original immutable property (let's call it "immutableProp") to a Dependent property like in the following,
classdef myclass
properties (SetAccess=immutable)
p;
end
properties (Dependent)
immutableProp; %imitates an immutable property
end
methods
function obj=myclass(pValue)
obj.p=pValue;
end
function out=get.immutableProp(obj)
out = obj.accessIt;
end
function out=accessIt(obj)
out=obj.p;
end
end
end
Then in your subclass, you simply overload accessIt() in a way that causes immutableProp to refer to q instead of p.
classdef mysubclass<myclass
properties (SetAccess=immutable)
q;
end
methods
function obj=mysubclass(pValue, qValue)
obj=obj@myclass(pValue);
obj.q=qValue;
end
function out=accessIt(obj)
out=obj.q;
end
end
end

その他の回答 (1 件)

Matt J
Matt J 2017 年 8 月 9 日
編集済み: Matt J 2017 年 8 月 9 日
Is it possible to somehow set immutable superclass properties in a subclass constructor?
Yes. The subclass constructor can call the superclass constructor and use that to set the immutable property, e.g.,
classdef myclass
properties (SetAccess=immutable)
p;
end
methods
function obj=myclass(pval)
obj.p=pval;
end
end
end
classdef mysubclass < myclass
methods
function obj=mysubclass(pval)
obj=obj@myclass(pval);
end
end
end
  1 件のコメント
Christian Veenstra
Christian Veenstra 2017 年 8 月 9 日
Thank you, but I should have rephrased my question - I was looking for a way to overwrite the value set by the superclass constructor. For now I've just added an extra argument to my superclass constructor - when I set that flag it does something different and sets the values as expected by the subclass.
This "works", but it seems like a sloppy workaround - like essentially the code for my subclass has spread into the superclass.

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

カテゴリ

Help Center および File ExchangeClass Introspection and Metadata についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by