💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
Enum constructors behave exactly as the other types with the exception that in a switch statement, it is possible to use just the constructor name in place of the complete one, 如果类型在编译时被知道甚至 enum 文件还没有被导入。 ~~~ // content of file style/FillKind.hx package style; enum FillKind { Solid(rgb : Color); Gradient(startColor : Color, endColor : Color); Texture(fileName : String); } ~~~ 先前代码中的 enum 可以用在Main.hx中,如下: ~~~ // content of file Main.hx import style.FillKind; class Main { static function main() { var c = Texture(“filename.jpg”); // removing the import statement, the above line should be // var c = style.FillKind.Texture(“filename.jpg”); switch(c) { // with or without the import statement, the constructor // name can be used without indicating the complete name // as long as the “c” type is known case Solid(color): // ... case Gradient(startColor, endColor): // ... case Texture(file): // ... } } } ~~~ Color 类型可以是一个 `enum`,一个 `typedef`,或者一个类,它的实现已经被忽略因为要理解 `import` 的逻辑并不重要。