打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Controlling Access to Class Members
On this page…
Basic Knowledge
Applications for Access Control Lists
Specify Access to Class Members
Properties with Access Lists
Methods with Access Lists
Abstract Methods with Access Lists
Basic Knowledge
The material presented in this section builds on an understanding of the following information:
 Related Topics
 Terminology and Concepts
 Possible Values for Access to Class Members
Applications for Access Control Lists
Access control lists enable you to control access to specific class properties, methods, and events, by specifying a list of classes to which you want to grant access to these class members.
This technique provides greater flexibility and control in the design of a system of classes. For example, use access control lists when you want to define parts of your class system in separate classes, but do not want to allow access to class members from outside the class system.
Specify Access to Class Members
Specify the classes that are allowed to access a particular class member in the member access attribute statement. For example:
methods (Access = {?ClassName1,?ClassName2,...})Use the class meta.class object to refer to classes in the access list. To specify more than one class, use a cell array of meta.classobjects. Use the package name when referring to classes that are in packages.
Note:   You must specify the meta.class objects explicitly (created with the ? operator), not as values returned by functions or other MATLAB expressions.
 Property Access
 Method Access
The following class declares an access list for the method Access attribute:
classdef MethodAccess methods (Access = {?ClassA, ?ClassB, ?MethodAccess}) function listMethod(obj) ... end endendThe MethodAccess class specifies the following method access:
Access to listMethod from an instance of MethodAccess by methods of the classes ClassA and ClassB.
Access to listMethod from an instance of MethodAccess by methods of subclasses of MethodAccess, because of the inclusion ofMethodAccess in the access list.
Subclasses of ClassA and ClassB are allowed to define a method named listMethod, and MethodAccess is allowed to redefinelistMethod. However, if MethodAccess was not in the access list, its subclasses could not redefine listMethod.
 Event Access
The following class declares an access list for the event ListenAccess attribute:
classdef EventAccess events (NotifyAccess = private, ListenAccess = {?ClassA, ?ClassB}) Event1 Event2 endendThe class EventAccess specifies the following event access:
Limits notify access for Event1 and Event2 to EventAccess; only methods of the EventAccess can trigger these events.
Gives listen access for Event1 and Event2 to methods of ClassA and ClassB. Methods of EventAccess, ClassA, and ClassB can define listeners for these events. Subclasses of MyClass cannot define listeners for these events. See Methods with Access Lists.
How MATLAB Interprets Attribute Values
Granting access to a list of classes restricts access to only:
The defining class
The classes in the list
Subclasses of the classes in the list
Including the defining class in the access list gives all subclasses of the defining class access.
MATLAB resolves references to classes in the access list only when the class is loaded. If MATLAB cannot find a class that is included in the access list, that class is effectively removed from the list.
An empty access list (that is, an empty cell array) is equivalent to private access.
Specifying Metaclass Objects
Use only the ? operator and the class name to generate the meta.class objects. Values assigned to the attributes cannot contain any other MATLAB expressions, including functions that return allowed attribute values:
meta.class objects
Cell arrays of meta.class objects
The values public, protected, or private
You must specify these values explicitly, as shown in the example code in this section.
Properties with Access Lists
These sample classes show the behavior of a property that grants read access (GetAccess) to a class. The GrantAccess class givesGetAccess to the NeedAccess class for the Prop1 property:
classdef GrantAccess properties (GetAccess = ?NeedAccess) Prop1 = 7; endendThe NeedAccess class defines a method that uses the value of the GrantAccess Prop1 value. The dispObj is defined as a Staticmethod, however, it could be an ordinary method.
classdef NeedAccess methods (Static) function dispObj(GrantAccessObj) % Display the value of Prop1 disp(['Prop1 is: ',num2str(GrantAccessObj.Prop1)]) end endendGet access to Prop1 is private so MATLAB returns an error:
>> a = GrantAccess;>> a.Prop1Getting the 'Prop1' property of the 'GrantAccess' class is not allowed.However, MATLAB allows access to Prop1 by the NeedAccess class:
>> NeedAccess.dispObj(a)Prop1 is: 7
Methods with Access Lists
Classes granted access to a method can:
Call the method using an instance of the defining class.
Define their own method with the same name (if not a subclass).
Override the method in a subclass only if the superclass defining the method includes itself or the subclass in the access list.
These sample classes show the behavior of methods called from methods of other classes that are in the access list. The classAcListSuper gives the AcListNonSub class access to itsm1 method:
classdef AcListSuper methods (Access = {?AcListNonSub}) function obj = m1(obj) disp ('Method m1 called') end endendBecause AcListNonSub is in the access list of m1, its methods can call m1 using an instance of AcListSuper:
classdef AcListNonSub methods function obj = nonSub1(obj,AcListSuper_Obj) % Call m1 on AcListSuper class AcListSuper_Obj.m1; end function obj = m1(obj) % Define a method named m1 disp(['Method m1 defined by ',class(obj)]) end endendCreate objects of both classes:
>> a = AcListSuper;>> b = AcListNonSub;Call the AcListSuper m1 method using an AcListNonSub method:
>> b.nonSub1(a);Method m1 calledCall the AcListNonSub m1 method:
>> b.m1;Method m1 defined by AcListNonSubSubclasses Without Access
Including the defining class in the access list for a method grants access to all subclasses derived from that class. When you derive from a class that has a method with an access list and that list does not include the defining class in the access list:
Subclass methods cannot call the superclass method because it is effectively private.
Subclasses cannot override the superclass method.
Subclass methods can call the superclass method indirectly using an instance of a class that is in the access list.
Nonsubclass methods of classes in the superclass method access list can call the superclass method using an instance of a subclass that is not in the superclass method access list.
For example, AcListSub is a subclass of AcListSuper. The AcListSuper class defines an access list for method m1. However, this list does not include AcListSuper, which would implicitly include all subclasses of AcListSuper in the access list:
classdef AcListSub < AcListSuper methods function obj = sub1(obj,AcListSuper_Obj) % Access m1 via superclass object (NOT ALLOWED) AcListSuper_Obj.m1; end function obj = sub2(obj,AcListNonSub_Obj,AcListSuper_obj) % Access m1 via object that is in access list (is allowed) AcListNonSub_Obj.nonSub1(AcListSuper_Obj); end endendAttempting to call the superclass m1 method results in an error because subclasses are not in the access list for the method:
>> a = AcListSuper;>> b = AcListNonSub;>> c = AcListSub;>> c.sub1(a);Error using AcListSuper/m1Cannot access method 'm1' in class 'AcListSuper'.Error in AcListSub/sub1 (line 4) AcListSuper_Obj.m1;The AcListSub sub2 method can call a method of a class that is on the access list for m1, and that method (nonSub1) does have access to the superclass m1 method:
>> c.sub2(b,a);Method m1 calledWhen subclasses are not included in the access list for a method, those subclasses cannot define a method with the same name. This behavior is not the same as cases in which the method's Access is explicitly declared as private.
For example, adding the following method to the AcListSub class definition produces an error when you attempt to instantiate the class.
methods (Access = {?AcListNonSub}) function obj = m1(obj) disp('AcListSub m1 method') endendIf you attempt to instantiate the class, MATLAB returns an error:
>> c = AcListSub;Error using AcListSubClass 'AcListSub' is not allowed to override the method 'm1' because neither it nor itssuperclasses have been granted access to the method by class 'AcListSuper'.The AcListNonSub class, which is in the m1 method access list, can define a method that calls the m1 method using an instance of theAcListSub class. While AcListSub is not in the access list for method m1, it is a subclass of AcListSuper.
For example, add the following method to the AcListNonSub class:
methods function obj = nonSub2(obj,AcListSub_Obj) disp('Call m1 via subclass object:') AcListSub_Obj.m1; endendCalling the nonSub2 method results in execution of the superclass m1 method:
>> b = AcListNonSub;>> c = AcListSub;>> b.nonSub2(c);Call m1 via subclass object:Method m1 calledThis is consistent with the behavior of any subclass object, which can be substituted for an instance of its superclass.
Abstract Methods with Access Lists
A class containing a method declared as Abstract is an abstract class. It is the responsibility of subclasses to implement the abstract method using the function signature declared in the class definition.
When an abstract method has an access list, only the classes in the access list can implement the method. A subclass that is not in the access list cannot implement the abstract method so that subclass is itself abstract.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Implementing the Singleton Pattern in Java
java equals使用instanceof还是getClass?
Duck Typing in Python
Stackoverflow上的Python问题精选
PyXPCOM - MDC
[[A alloc] init] 和 [[[A class] alloc] init]的区别
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服