Skip to content

常量与枚举 ​


枚举,一种由若干命名常量组成的特殊用户自定义类型。

语法 ​

vb
Enum [typename [ Explicit ] ]

symbolname [= expression] [, ...]
...

End Enum

参数 ​

typename

Enum 的名称

symbolname

常量的名称

expression

常量表达式

Explicit

要求符号必须通过 typename.symbolname 显式引用

说明 ​

枚举是由一组命名整数常量组成的用户自定义类型。

枚举用于定义序列和状态,特别是当这些状态之间存在自然递进关系时。

枚举提供上下文来描述一系列值,这些值通过符号表示为命名常量。

与命名空间不同,枚举也可以在任何作用域块中定义,其符号仅在声明枚举的作用域内可见。

枚举有助于编写更易维护的代码,因为它们是符号化的,允许你在使用显式名称的同时处理整数值。

枚举是值类型,这意味着它们包含自己的值,不能继承或被继承。

Enum 不能包含任何成员过程或成员数据(只有符号),但可以通过包含(命名或匿名)嵌入到 Type 中。

用法 ​

枚举中的每个符号都隐式分配一个整数值(正数或负数),对应于其在枚举中值的顺序位置。

每个符号都被视为常量。

默认情况下,第一个值分配为 0,下一个分配为 1,依此类推。

但你可以显式设置任何符号的值(下一个符号将隐式设置为前一个值加上 1 的增量)。

给符号赋予的值不必是唯一的。

当枚举被限定为 Explicit 时,对任何 symbolname 的访问必须始终以 typename 为前缀。

前缀也可用于解决与其他实体的歧义。

Enum 实例可以像任何用户自定义类型实例一样传递给过程(包括用于定义重载运算符的定义)。

Enum 实例的大小始终等于 Integer 的大小(无论为编译器赋值定义了多少符号)。

Enum 实例(或 Enum 符号)可以隐式转换为整数。

注意:

  • 在许多语言中,没有整数变量可以隐式转换为有作用域的枚举类型实例(反之亦然),后者是强类型的(声明等同于 FreeBASIC 中限定为 Explicit 的 Enum),此类转换需要显式转换。

  • 但 FreeBASIC 接受它(即使数值与任何已定义的 Enum 符号不匹配)。这只是 FreeBASIC 在普通 Enum 功能方面众多不足之一。

  • 因此,声明真正的 ENUM 实例(其大小与 INTEGER 相同)没有太大意义,否则可以声明任何预建整数变量来代替。

  • 如果不使用 Explicit 限定符(未强制命名空间前缀),使用 ENUM 结构与简单的离散常量列表相比没有太大意义,除了值的自动递增。

示例 ​

简单使用示例:

start GeSHi

vb
Enum Colors
    black
    blue
    green
    cyan
    red
    pink
    yellow
    grey
    dark_grey
    bright_blue
    bright_green
    bright_cyan
    bright_red
    bright_pink
    bright_yellow
    white
End Enum

Sub print_fbc (ByVal foreground As Colors, ByVal background As Colors)
    Color foreground, background
    Print " " & __FB_SIGNATURE__ & " "
End Sub

Dim As Colors std_foreground, std_background
std_foreground = LoWord(Color())
std_background = HiWord(Color())

Dim As Colors my_foreground, my_background
my_foreground = bright_yellow
my_background = cyan

print_fbc(my_foreground, my_background)

Color std_foreground, std_background
Print "end"

Sleep

end GeSHi

相同结果,但使用 Type 接口 Enum 以在为 Enum 实例赋值数值时强制显式转换(参见上面的"注意"):

start GeSHi

vb
Enum Colors Explicit
    black
    blue
    green
    cyan
    red
    pink
    yellow
    grey
    dark_grey
    bright_blue
    bright_green
    bright_cyan
    bright_red
    bright_pink
    bright_yellow
    white
End Enum

Type Console_Colors
    Public:
        Declare Property foreground () As Colors
        Declare Property foreground (ByVal c As Colors)
        Declare Property background () As Colors
        Declare Property background (ByVal c As Colors)
    Private:
        Dim As Colors _foreground
        Dim As Colors _background
End Type

Property Console_Colors.foreground () As Colors
    Return This._foreground
End Property

Property Console_Colors.foreground (ByVal c As Colors)
    This._foreground = c
End Property

Property Console_Colors.background () As Colors
    Return This._background
End Property

Property Console_Colors.background (ByVal c As Colors)
    This._background = c
End Property

Sub print_fbc (ByVal foreground As Colors, ByVal background As Colors)
    Color foreground, background
    Print " " & __FB_SIGNATURE__ & " "
End Sub

Dim As Console_Colors std_colors
std_colors.foreground = Cast(Colors, LoWord(Color()))  '' explicit cast mandatory because of property declaration
std_colors.background = Cast(Colors, HiWord(Color()))  '' explicit cast mandatory because of property declaration

Dim As Console_Colors my_colors
my_colors.foreground = Colors.bright_yellow
my_colors.background = Colors.cyan

print_fbc(my_colors.foreground, my_colors.background)

Color std_colors.foreground, std_colors.background
Print "end"

Sleep

end GeSHi

另请参阅 ​

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系