Components of a Class - MATLAB & Simulink (original) (raw)
Class Building Blocks
MATLAB® organizes class definition code into modular blocks, delimited by keywords. All keywords have an associated end
statement:
classdef...end
— Definition of all class componentsproperties...end
— Declaration of property names, specification of property attributes, assignment of default valuesmethods...end
— Declaration of method signatures, method attributes, and function codeevents...end
— Declaration of event name and attributesenumeration...end
— Declaration of enumeration members and enumeration values for enumeration classes
properties
, methods
, events
, and enumeration
are keywords only within aclassdef
block.
Class Definition Block
The classdef
block contains the class definition within a file that starts with the classdef
keyword and terminates with theend
keyword.
classdef (ClassAttributes) ClassName < SuperClass ... end
For example, this classdef
defines a class calledMyClass
that subclasses the handle
class. The class is also defined as sealed, so you cannot use inherit from this class.
classdef (Sealed) MyClass < handle ... end
See classdef for more syntax information.
Properties Block
A properties
block contains property definitions, including optional initial values. Use a separate block for each unique set of attribute specifications. Each properties block starts with the properties
keyword and terminates with the end
keyword.
properties (PropertyAttributes) PropertyName size class {validators} = DefaultValue end
For example, this class defines a private property Prop1
of typedouble
with a default value.
classdef MyClass properties (SetAccess = private) Prop1 double = 12 end ... end
See Initialize Property Values for more information.
Methods Block
A methods
block contains function definitions for the class methods. Use a separate block for each unique set of attribute specifications. Each methods block starts with the methods
keyword and terminates with theend
keyword.
methods (MethodAttributes) function obj = MethodName(arg1,...) ... end
For example, this class defines a protected method MyMethod
.
classdef MyClass methods (Access = protected) function obj = myMethod(obj,arg1) ... end end end
See Method Syntax for more information.
MATLAB differs from languages like C++ and Java® in that you must explicitly pass an object of the class to the method.
Using the MyClass
example, call MyMethod
using the object obj
of the class and either function or dot syntax:
obj = MyClass; r = MyMethod(obj,arg1); r = obj.MyMethod(arg1);
For more information, see Method Invocation.
Events Block
The events
block (one for each unique set of attribute specifications) contains the names of events that this class declares. Theevents
block starts with the events
keyword and terminates with the end
keyword.
classdef ClassName events (EventAttributes) EventName end ... end
For example, this class defined an event called StateChange
withListenAccess
set to protected
.
classdef EventSource events (ListenAccess = protected) StateChanged end ... end
See Events for more information.
Attribute Specification
Attribute Syntax
Attributes modify the behavior of classes and class components (properties, methods, and events). Attributes enable you to define useful behaviors without writing complicated code. For example, you can create a read-only property by setting its SetAccess
attribute to private but leaving itsGetAccess
attribute set to public.
properties (SetAccess = private) ScreenSize = getScreenSize end
All class definition blocks (classdef
,properties
, methods
, andevents
) support specific attributes. All attributes have default values. Specify attribute values only in cases where you want to change from the default value.
Note
Specify the value of a particular attribute only once in any component block.
Attribute Descriptions
For lists of supported attributes, see:
Attribute Values
When you specify attribute values, those values affect all the components defined within the defining block. Defining properties with different attribute settings requires multiple properties blocks. Specify multiple attributes in a comma-separated list.
properties (SetObservable = true) AccountBalance end
properties (SetAccess = private, Hidden = true) SSNumber CreditCardNumber end
Simpler Syntax for true
/false
Attributes
You can use a simpler syntax for attributes whose values aretrue
or false
. The attribute name alone implies true and adding the not operator (~) to the name implies false. For example, these two ways of defining a static methods block are equivalent.
methods (Static) ... end
methods (Static = true) ... end
Similarly, these three ways of defining a nonstatic methods block are equivalent. All attributes that take a logical value have a default value offalse
, so you can omit the attribute to get the default behavior.
methods ... end
methods (~Static) ... end
methods (Static = false) ... end
Enumeration Classes
Enumeration classes are specialized classes that define a fixed set of names representing a single type of value. Enumeration classes use anenumeration
block that contains the enumeration members defined by the class.
The enumeration block starts with the enumeration
keyword and terminates with the end
keyword.
classdef ClassName < SuperClass enumeration EnumerationMember end ... end
For example, this class defines two enumeration members that represent the logical values false
and true
.
classdef Boolean < logical enumeration No (0) Yes (1) end end
See Define Enumeration Classes for more information.