フィルターのクリア

Editing superclass properties from subclasses

3 ビュー (過去 30 日間)
Simon
Simon 2013 年 9 月 11 日
Hi!
I have a question related to the Matlab example http://www.mathworks.com/help/matlab/matlab_oop/a-simple-class-hierarchy.html. For testing I use:
ds = DocSavings;
db = DocBond;
I can change the superclass property "Description" with
ds.Description = 'test';
However, listing "db" properties shows an empty "Description" string. Is there a way to synchronize superclass properties? Is using events and listeners of handle classes the only possible way?
Maybe: Another way is to use the subclasses as normal classes and define objects of these classes as properties of the former superclass?
Is there a better solution?

採用された回答

per isakson
per isakson 2013 年 9 月 13 日
編集済み: per isakson 2013 年 9 月 14 日
Why do you want different objects to share ( "synchronize" ) property values? However, without looking at the documentation you refer to.
super = SuperClass;
sub = SubClass;
sub.handle.Description = 'Hello';
super.handle.Description
super.handle.Description = 'World';
sub.handle.Description
returns
ans =
Hello
ans =
World
where
classdef SuperClass < handle
properties
handle = DescriptionContainer();
end
end
and
classdef DescriptionContainer < handle
properties
Description
end
end
and
classdef SubClass < SuperClass
properties
another_property
end
end
.
With the help of a dependent property together with set and get methods, I think it would be possible to hide (Access=protected) the property, which I call "handle".
.
The word, "Editing", in the subject line makes me unsure regarding your intent.
"Maybe: Another way ..." sounds strange to me. Wouldn't that overly complicate the dependencies.
"events and listeners" if nothing else it complicates the debugging.
  2 件のコメント
Simon
Simon 2013 年 9 月 16 日
Hi!
Thank you for your reply!
"Editing" in the subject line maybe better replaced with sharing or synchronizing, you're right.
Why do you want different objects to share ( "synchronize" ) property values?
Suppose I have a superclass for reading/writing to a file, with a property called "FileName". If I create two subclasses to use this superclass, each subclass has its own instance of the superclass property "FileName". But I want to write the individual properties of the subclasses to the same file (automatically). This is true if I have another class (like in your example) storing the filename (the description in your example). This is the second solution I tried to explain in my "Maybe: Another way ..." paragraph. But I realize it was quite confusing ;-)
I think I should re-think the problem ...
per isakson
per isakson 2013 年 9 月 16 日
編集済み: per isakson 2013 年 9 月 16 日
Comments:
  • What you describe makes me think of a logger. See the FEX-contributions log4m and log4matlab
  • Make some experiments with running code before you settle for a solution.
  • "Is there a better solution?" I think the solution I outline answers your question.

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

その他の回答 (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