Create an enumeration class by adding anenumeration
block to a class definition. For example, theWeekDays
班级列举了一周中的一组。
classdefWeekDaysenumeration周一周二周三周四周五endend
执行MATLAB®code in the following sections, place theWeekDays
班级定义.m
在您的路径上归档。
Refer to an enumeration member using the class name and the member name:
ClassName.MemberName
例如,分配枚举成员工作日。TUESDAY
到变量今天
:
今天=工作日。tuesday;
今天
is a variable of classWeekDays
:
whos
名称大小字节类属性今天1x1 104工作日
今天
今天=星期二
If an enumeration class specifies a superclass, convert an enumeration object to the superclass by passing the object to the superclass constructor. However, the superclass constructor must be able to accept its own class as input and return an instance of the superclass. MATLAB built-in numeric classes, such asuint32
, allow this conversion.
例如,Bearing
class derives from theuint32
built-in class:
classdefBearing < uint32enumeration不rth (0) East (90) South (180) West (270)endend
Assign theBearing.East
member to the variablea
:
a = Bearing.East;
Passa
到超级类构造函数并返回uint32
value:
b = uint32(a); whos
Name Size Bytes Class Attributes a 1x1 60 Bearing b 1x1 4 uint32
Theuint32
构造函数接受子类的对象Bearing
并返回和班级的对象uint32
.
Define methods in an enumeration class like any MATLAB class. For example, here is theWeekDays
class with a method calledisMeetingDay
added:
classdefWeekDaysenumeration周一周二周三周四周五endmethods功能tf = ismeetingday(obj)tf =〜(weekdays.tuesday == obj);endendend
CallisMeetingDay
with an instance of theWeekDays
class:
今天=工作日。tuesday;今天.isMeetingDay
ans = 0
Use the enumeration member directly as input to the method:
isMeetingDay(WeekDays.Wednesday)
ans = 1
当您必须存储与枚举成员有关的数据时,将属性添加到枚举类中。在类构造函数中设置属性值。例如,SyntaxColors
class defines three properties whose values the constructor assigns to the values of the input arguments when you reference a class member.
classdefSyntaxColors特性R G Bendmethods功能c = SyntaxColors(r, g, b) c.R = r; c.G = g; c.B = b;endendenumerationError (1, 0, 0) Comment (0, 1, 0) Keyword (0, 0, 1) String (1, 0, 1)endend
当您参考枚举成员时,构造函数将属性值初始化:
e = SyntaxColors.Error; e.R
ans = 1
BecauseSyntaxColors
is a value class (it does not derive fromhandle
),只有类构造函数可以设置属性值:
e.R = 0
You cannot set the read-only property 'R' of SyntaxColors.
For more information on enumeration classes that define properties, seeMutable Handle vs. Immutable Value Enumeration Members.
Each statement in an enumeration block is the name of an enumeration member, optionally followed by an argument list. If the enumeration class defines a constructor, MATLAB calls the constructor to create the enumerated instances.
MATLAB为所有未明确定义构造函数的枚举类提供了默认构造函数。默认构造函数创建了枚举类的实例:
Using no input arguments, if the enumeration member defines no input arguments
Using the input arguments defined in the enumeration class for that member
例如,input arguments for theBool
class are0
forBool.No
和1
forBool.Yes
.
classdefBool < logicalenumeration不(0) Yes (1)endend
The values of0
和1
are of classlogical
because the default constructor passes the argument to the first superclass. That is, this statement:
n = Bool.No;
Results in a call tological
that is equivalent to the following statement in a constructor:
功能obj = Bool(val) obj@logical(val)end
MATLAB passes the member argument only to the first superclass. For example, supposeBool
derived from another class:
classdefBool < logical & MyBoolenumeration不(0) Yes (1)endend
TheMyBool
class can add some specialized behavior:
classdefMyBoolmethods功能boolValues = testBools(obj)...endendend
The defaultBool
constructor behaves as if defined like this function:
Argument passed to first superclass constructor
不arguments passed to subsequent constructors
功能obj = Bool(val) obj@logical(val) obj@MyBoolend