Modify attributes of inherited property
9 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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
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.
更多回答(1 个)
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
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Methods 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!