Define Enumeration Classes
Enumeration Class
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
在您的路径上归档。
Construct an Enumeration Member
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工作日
今天
今天=星期二
Convert to Superclass Value
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 < uint32enumerationNorth (0) East (90) South (180) West (270)endend
Assign the轴承。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
使用一种称为的方法ismeetingday
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
Define Properties in Enumeration Classes
当您必须存储与枚举成员有关的数据时,将属性添加到枚举类中。在类构造函数中设置属性值。例如,语法
class defines three properties whose values the constructor assigns to the values of the input arguments when you reference a class member.
classdef语法特性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
Because语法
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.
Enumeration Class Constructor Calling Sequence
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
使用该成员的枚举类中定义的输入参数
例如,input arguments for theBool
class are0
forBool.No
和1
forBool.Yes
.
classdefBool < logicalenumerationNo (0) Yes (1)endend
The values of0
和1
are of classlogical
因为默认构造函数将参数传递给第一个超类。也就是说,这个说法:
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 & MyBoolenumerationNo (0) Yes (1)endend
TheMyBool
class can add some specialized behavior:
classdefMyBoolmethods功能boolValues = testBools(obj)...endendend
默认值Bool
constructor behaves as if defined like this function:
Argument passed to first superclass constructor
No arguments passed to subsequent constructors
功能obj = Bool(val) obj@logical(val) obj@MyBoolend