Modify attributes of inherited property

9 ビュー (過去 30 日間)
Luuk Poort
Luuk Poort 2021 年 11 月 2 日
コメント済み: Luuk Poort 2021 年 11 月 3 日
I defined a subclass which inherits a built-in class (mechss) and extends it with some additional properties and methods. Because of my additional properties, the terminology becomes somewhat confusing and I would like to change the property names of the superclass. For example, I would like to change the property C to be called L.
As far as I understand, this is not possible, but I cán define the new property L and relate it to C using get and set methods. This leaves me with two identical properties however, and I would like to hide C and make its access protected.
Is something like this possible?

採用された回答

Matt J
Matt J 2021 年 11 月 2 日
編集済み: Matt J 2021 年 11 月 2 日
Not directly, but you can overload the display() method, customizing it so that C is never displayed at the command line. Similarly, you can customize subsref() and subsasgn() so that dot indexing outside the class does not allow access to C.
  3 件のコメント
Matt J
Matt J 2021 年 11 月 2 日
For that matter, if you're overloading these methods, you don't have to create a 2nd property L. You can just customize the display and susbref/subsasgn so that when the alias obj.L is invoked, the value of C is accessed instead.
Luuk Poort
Luuk Poort 2021 年 11 月 2 日
Even better, thank you Matt!

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

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2021 年 11 月 2 日
編集済み: Sean de Wolski 2021 年 11 月 2 日
You could instead author your own class that passes through to a mechss object it stores as a private property. Then you can call the properties whatever you want.
  4 件のコメント
Sean de Wolski
Sean de Wolski 2021 年 11 月 2 日
The benefit of this approach is if MathWorks changes something in mechss (e.g. overloads indexing which it could), your solution won't break. And it's likely faster than overloading indexing.
Is the main challenge just naming confusion? If yes, maybe consider less confusing names, or names with a pattern/prefix that's easy to distinquish?
E.g., making things up
classdef electromechss < mechss
properties
electroC
electroL
end
methods
electroWhatever1
electroWhatever2
end
end
Luuk Poort
Luuk Poort 2021 年 11 月 3 日
Thank you for both of your input.
So, option 1 is to inherit the mechss class and overload the subsref and subsasgn methods to access the inherited properties with any name I would like, e.g. C can be obtained by .L, while inherited methods can be accessed both with dot notation or function notation.
With option 2, my class is not a subclass, but stores the mechss object as a private property. This is likely faster and more robust to future changes in built-in code. Methods of mechss can be accessed with dot notation by overloading the dot-indexing to be forwarded to the mechss object. To enable function notation, I need to define a new method for each method of mechss to forward it to the mechss object.
I hope I am correct with this and I will test what works out best for me. Unfortunately I cannot accept two answers.
Regarding the naming confusion: I would like object properties to match terminology in my field and in my own documentation to avoid confusion with users, when I will share my implementation in the future.

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

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by