Virtools脚本语言(VSL)教程 - 枚举

<div dir="ltr">
<p>enum关键字指定了一个枚举类型。枚举类型是一种用户定义的类型,由一套叫做计数器(enumerator)的赋之以名称的常量组成。缺省情况下,第一个计数器有一个为0的值,每个后续的计数器都比前一个的值更大(除非你显示地为特定计数器指定一个值)。</p>
<p>定义语法:</p>
<p>enum <em>enumName</em> { <em>enumMember</em> , ... };</p>
<p>enumMember :
<br><em>enumMembersName</em>
<br>

<br><em>enumMembersName</em> = <em>integerValue</em></p>
<p>实例</p>
<pre>
enum CharacterType<br>
{<br>
       HUMAIN,         <span>// 0</span>
        MONSTER,        <span>// 1</span>
        GHOST,          <span>// 2</span>
        BIRD            <span>// 3</span>
};

enum DisplacementMode
{
        WALK,
        FLY,
        TELEPORT
};

<p>然后你可以在你的函数中使用所定义的常量。</p>
<pre>
void ManageCharacterMove(Entity3D charac, CharacterType type, DisplacementMode disp)<br>
{<br>
   switch (type)<br>
       {<br>
           case HUMAIN :<br>
                       if (disp == WALK)<br>
                   {<br><span>// ...</span>
                        }
                        else if (disp == FLY)
                        {
                                <span>// ...</span>
                        }
                        else
                        {
                                <span>// ...</span>
                        }
                break;

                case MONSTER :
                        <span>// ...</span>
                break;

                <span>// ...</span>
        }
}

<p>计数器不需要具有唯一的值。每个计数器的名称都以常量方法对待,且在enum被定义的变量范围内唯一。</p>
<pre>
enum week <br>
{ <br>
        Mon = 1,        <span>// forces start at value 1</span>
        Tue,            <span>// 2</span>
        Wed,            <span>// 3</span>
        Thu,            <span>// etc</span>
        Fri,
        Sat,
        Sun
};

enum weekend
{
        Sat, <span>// error, Sat redefinition</span>
        Sun <span>// error, Sun redefinition</span>
};
</pre></pre></pre>
</div>

你可能感兴趣的:(tools)