Are properties with Access class lists private?
显示 更早的评论
I have the simple class hierarchy below:
classdef myclass
properties (Access = {?ClassA})
p=1;
end
methods
function pdisp(obj)
obj.p
end
end
end
classdef mysubclass<myclass
% properties (Access=public)
% p
% end
methods
function pdisp(obj)
obj.p
end
end
end
In this set-up, it appears the p property is effectively private from an inheritance perspective. We can see that mysubclass does not have access to it,
>> sobj = mysubclass;
>> sobj.pdisp
No public property 'p' for class 'mysubclass'.
Error in mysubclass/pdisp (line 10)
obj.p
^^^^^
The documentation does mention that access to properties is not inherited when a class access list is used,
"Access lists are not inherited, so subclasses of PropertyAccess do not have access to Prop2 unless they explicitly define that access."
But what does it mean here to "explicitly define access"? When a parent class has a private property, it is supposed to be possible to redefine that property in the subclass. However, when I uncomment the property redefinition section in mysubclass, it produces an error.
>> sobj.pdisp
Cannot define property 'p' in class 'mysubclass' because the property has already been defined in the
superclass 'myclass'.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Class Syntax and File Organization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
