森口万*_*万太郎 0 dart flutter sealed-class
https://www.youtube.com/watch?v=2Cl0C-9dK48&list=PLjxrf2q8roU1fRV40Ec8200rX6OuQkmnl
\n类型 促销 | 解码颤振
\n在上面的视频中,有以下解释。
\n\n\nDart 没有密封类。这意味着每个类都可以扩展甚至实现。
\n
我有时会看到术语“密封类”,但我在 Dart 中找不到该术语的定义。\nDart 中是否有任何文档或明确定义的内容?
\n\xe2\x86\x93\n https://dart.dev/guides/language/language-tour#enumerated-types
\n\n\n注意:所有枚举都会自动扩展 Enum 类。它们也是密封的,这意味着它们不能被子类化、实现、混合或以其他方式显式实例化。
\n
我搜索的时候发现了上面这句话,但这到底是“密封类”的定义吗?
\n此链接解释了 Dart 中的密封类。
// UnitedKingdom --+-- NorthernIreland
// |
// +-- GreatBritain --+-- England
// |
// +-- Scotland
// |
// +-- Wales
sealed class UnitedKingdom {}
class NorthernIreland extends UnitedKingdom {}
sealed class GreatBritain extends UnitedKingdom {}
class England extends GreatBritain {}
class Scotland extends GreatBritain {}
class Wales extends GreatBritain {}
Run Code Online (Sandbox Code Playgroud)
不仅标记 UnitedKingdom 密封,而且标记 GreatBritain 意味着所有这些开关都是详尽的:
test1(UnitedKingdom uk) {
switch (uk) {
case NorthernIreland(): print('Northern Ireland');
case GreatBritain(): print('Great Britain');
}
}
Run Code Online (Sandbox Code Playgroud)
截至今天,状态为Progress。
编辑:以上链接现已移至新位置,当前状态为Accepted。
更新:谢谢@Abion47,密封类现已合并,这是该票证的 GitHub链接。
| 归档时间: |
|
| 查看次数: |
3334 次 |
| 最近记录: |