Documentation

Define Enumeration Classes

Enumeration Class

Create an enumeration class by adding anenumerationblock 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.

例如,Bearingclass derives from theuint32built-in class:

classdefBearing < uint32enumeration不rth (0) East (90) South (180) West (270)endend

Assign theBearing.Eastmember to the variablea:

a = Bearing.East;

Passa到超级类构造函数并返回uint32value:

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 theWeekDaysclass with a method calledisMeetingDayadded:

classdefWeekDaysenumeration周一周二周三周四周五endmethods功能tf = ismeetingday(obj)tf =〜(weekdays.tuesday == obj);endendend

CallisMeetingDaywith an instance of theWeekDaysclass:

今天=工作日。tuesday;今天.isMeetingDay
ans = 0

Use the enumeration member directly as input to the method:

isMeetingDay(WeekDays.Wednesday)
ans = 1

Define Properties in Enumeration Classes

当您必须存储与枚举成员有关的数据时,将属性添加到枚举类中。在类构造函数中设置属性值。例如,SyntaxColorsclass 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

BecauseSyntaxColorsis 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

  • Using the input arguments defined in the enumeration class for that member

例如,input arguments for theBoolclass are0forBool.No1forBool.Yes.

classdefBool < logicalenumeration不(0) Yes (1)endend

The values of01are of classlogicalbecause the default constructor passes the argument to the first superclass. That is, this statement:

n = Bool.No;

Results in a call tologicalthat 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, supposeBoolderived from another class:

classdefBool < logical & MyBoolenumeration不(0) Yes (1)endend

TheMyBoolclass can add some specialized behavior:

classdefMyBoolmethods功能boolValues = testBools(obj)...endendend

The defaultBoolconstructor 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

相关话题

这个话题有帮助吗?