导入(Import)
import 决定外部名字怎么进入本文件:有的给前缀,有的给短名,有的只激活扩展方法。九种形态各有分工。
形态总表
| 写法 | 得到什么 | 典型用途 |
|---|---|---|
import Mod; | 前缀 Mod(不注入短名) | 限定访问 Mod.f() |
import Pkg::Mod; | 跨包模块前缀 | 跨包限定访问 |
import Mod as A; | 前缀改名 A | 消歧、缩短 |
import Mod.*; | 模块全部顶层符号短名 | 短名直用 |
import Mod.{X, Y as Z}; | 所选符号短名(可逐个别名) | 精确注入 |
import Mod.X; | 相当于 Mod.{ X } | 精确注入 |
import Mod.X as Y; | 相当于 Mod.{X as Y} | 精确注入 |
import Mod.extension; | 无名(仅激活) | 只为扩展方法 |
import Pkg::*; | 包内全部模块名作前缀 | 跨包批量 |
前缀、通配与别名
import Math.*;
import Collections.*;
func Bigger<T>(a: T, b: T) where T: Ordered -> T
{
if a < b
{
return b;
}
return a;
}
func main() -> int
{
print(Bigger(3, 9));
print(Math.Max(2, 5), Math.Abs(-4));
val s = new set<string>();
s.Add("甲");
print(s.Contains("甲"));
return 0;
}9
5 4
True编写到此Written up to here
本页其余部分尚未完成,内容随时补充。The rest of this page is not written yet; more content will follow.
最后更新于 2026年10月10日
