- Euphoria Tutorial
- Euphoria - Home
- Euphoria - Overview
- Euphoria - Environment
- Euphoria - Basic Syntax
- Euphoria - Variables
- Euphoria - Constants
- Euphoria - Data Types
- Euphoria - Operators
- Euphoria - Branching
- Euphoria - Loop Types
- Euphoria - Flow Control
- Euphoria - Short Circuit
- Euphoria - Sequences
- Euphoria - Date & Time
- Euphoria - Procedures
- Euphoria - Functions
- Euphoria - Files I/O
- Euphoria Useful Resources
- Euphoria - Quick Guide
- Euphoria - Library Routines
- Euphoria - Useful Resources
- Euphoria - Discussion
欣快感 - 常量
常量也是被赋予初始值的变量,该初始值在程序的生命周期中永远不会改变。Euphoria 允许使用 Constant 关键字定义常量,如下所示 -
constant MAX = 100 constant Upper = MAX - 10, Lower = 5 constant name_list = {"Fred", "George", "Larry"}
任何表达式的结果都可以分配给常量,即使是涉及调用先前定义的函数的表达式,但一旦进行分配,常量变量的值就被“锁定”。
不能在子例程内声明常量。没有作用域修饰符的常量的作用域从声明开始,到声明它的文件末尾结束。
例子
#!/home/euphoria-4.0b2/bin/eui constant MAX = 100 constant Upper = MAX - 10, Lower = 5 printf(1, "Value of MAX %d\n", MAX ) printf(1, "Value of Upper %d\n", Upper ) printf(1, "Value of Lower %d\n", Lower ) MAX = MAX + 1 printf(1, "Value of MAX %d\n", MAX )
这会产生以下错误 -
./test.ex:10 <0110>:: may not change the value of a constant MAX = MAX + 1 ^ Press Enter
如果从示例中删除最后两行,则会产生以下结果 -
Value of MAX 100 Value of Upper 90 Value of Lower 5
枚举_
枚举值是一种特殊类型的常量,其中第一个值默认为数字 1,之后的每一项都递增 1。枚举只能采用数字值。
枚举不能在子例程内声明。没有范围修饰符的枚举的范围从声明开始,到声明它的文件末尾结束。
例子
#!/home/euphoria-4.0b2/bin/eui enum ONE, TWO, THREE, FOUR printf(1, "Value of ONE %d\n", ONE ) printf(1, "Value of TWO %d\n", TWO ) printf(1, "Value of THREE %d\n", THREE ) printf(1, "Value of FOUR %d\n", FOUR )
这将产生以下结果 -
Value of ONE 1 Value of TWO 2 Value of THREE 3 Value of FOUR 4
您可以通过为其指定数值来更改任何一项的值。后续值始终是前一个值加一,除非它们也被分配了默认值。
#!/home/euphoria-4.0b2/bin/eui enum ONE, TWO, THREE, ABC=10, XYZ printf(1, "Value of ONE %d\n", ONE ) printf(1, "Value of TWO %d\n", TWO ) printf(1, "Value of THREE %d\n", THREE ) printf(1, "Value of ABC %d\n", ABC ) printf(1, "Value of XYZ %d\n", XYZ )
这会产生以下结果 -
Value of ONE 1 Value of TWO 2 Value of THREE 3 Value of ABC 10 Value of XYZ 11
序列使用整数索引,但使用枚举,您可以编写如下代码 -
enum X, Y sequence point = { 0,0 } point[X] = 3 point[Y] = 4