MATLAB Object-Oriented Programming

(Joyce) #1

Properties with Access Lists


These sample classes show the behavior of a property that grants read access
(GetAccess) to a class. The GrantAccess class gives GetAccess to the NeedAccess
class for the Prop1 property:


classdef GrantAccess
properties (GetAccess = ?NeedAccess)
Prop1 = 7
end
end


The NeedAccess class defines a method that uses the value of the GrantAccess Prop1
value. The dispObj method is defined as a Static method, however, it could be an
ordinary method.


classdef NeedAccess
methods (Static)
function dispObj(GrantAccessObj)
disp(['Prop1 is: ',num2str(GrantAccessObj.Prop1)])
end
end
end


Get access to Prop1 is private so MATLAB returns an error if you attempt to access the
property from outside the class definition. For example, from the command line:


a = GrantAccess;
a.Prop1


Getting 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.


Class Members Access
Free download pdf