在本教程中,我们将研究Kotlin密封类. 他们是什么? 他们的用途是什么? 我们将在下面解决所有这些问题。
科特林密封类
In layman terms, as the name suggests, sealed classes are sealed or closed, hence making them restricted. Sealed classes are used for representing restricted class hierarchies wherein the object or the value can have value only among one of the types, thus fixing your type hierarchies. Sealed classes are commonly used in cases, where you know what a given value to be only among a given set of options.
使用 Kotlin 密封类
Kotlin 中的密封类以如下方式实施。
1sealed class A{
2 class B : A()
3 class C : A()
4}
要指定一个密封类,您需要添加修改器密封
。 密封类不能实例化,因此是暗示抽象的。
1fun main(args: Array<String>)
2{
3 var a = A() //compiler error. Class A cannot be instantiated.
4}
默认情况下,密封类的构建者是私有类,密封类的所有子类必须在同一个文件中声明。
1sealed class A{
2 class B : A()
3 {
4 class E : A() //this works.
5 }
6 class C : A()
7
8 init {
9 println("sealed class A")
10 }
11
12}
13
14class D : A() //this works
15{
16class F: A() //This won't work. Since sealed class is defined in another scope.
17}
** 通过构建者创建一个密封的类。
1sealed class A(var name: String){
2 class B : A("B")
3 class C : A("C")
4}
5
6class D : A("D")
1fun main(args: Array<String>) {
2
3 var b = A.B()
4 var d = D()
5}
1fun main(args: Array<String>) {
2
3 val e = A.E("Anupam")
4 println(e) //prints E(name=Anupam)
5
6 var d = A.D
7 d.name() //prints Object D
8}
9
10sealed class A{
11 class B : A()
12 class C : A()
13 object D : A()
14 {
15 fun name()
16 {
17 println("Object D")
18 }
19 }
20 data class E(var name: String) : A()
21
22}
enum 和密封类的区别
在Kotlin中,密封类可以被称为在类固醇上的Enum类。密封类允许我们创建不同类型的实例,与(Enums)(/社区/教程/kotlin-enum-class)不同,这限制了我们在所有enum常数中使用相同类型。
1enum class Months(string: String){
2January("Jan"), February(2),
3}
Enum类允许所有常数只有一种类型,这里是密封类通过允许多个实例来拯救我们的地方。
1sealed class Months {
2 class January(var shortHand: String) : Months()
3 class February(var number: Int) : Months()
4 class March(var shortHand: String, var number: Int) : Months()
5}
如何在您的项目中使用密封类
功能?在应用程序类似的新闻源中,您可以创建三个不同类型的状态、图像和视频帖子,如下所示。
1sealed class Post
2{
3 class Status(var text: String) : Post()
4 class Image(var url: String, var caption: String) : Post()
5 class Video(var url: String, var timeDuration: Int, var encoding: String): Post()
6}
这在Enum课程中是不可能的。
经典课堂,何时
密封类通常用于何时
声明,因为每个子类和它们的类型都作为一个案例。 此外,我们知道密封类限制类型,因此,else
声明中的else
部分可以很容易地删除。
1sealed class Shape{
2 class Circle(var radius: Float): Shape()
3 class Square(var length: Int): Shape()
4 class Rectangle(var length: Int, var breadth: Int): Shape()
5}
6
7fun eval(e: Shape) =
8 when (e) {
9 is Shape.Circle -> println("Circle area is ${3.14*e.radius*e.radius}")
10 is Shape.Square -> println("Square area is ${e.length*e.length}")
11 is Shape.Rectangle -> println("Rectagle area is ${e.length*e.breadth}")
12 }
让我们在我们的主要
函数中执行eval
函数,如下所示。
1fun main(args: Array<String>) {
2
3 var circle = Shape.Circle(4.5f)
4 var square = Shape.Square(4)
5 var rectangle = Shape.Rectangle(4,5)
6
7 eval(circle)
8 eval(square)
9 eval(rectangle)
10 //eval(x) //compile-time error.
11
12}
13
14//Following is printed on the console:
15//Circle area is 63.585
16//Square area is 16
17//Rectangle area is 20
注意: is
修改器会检查类型是否属于以下类型。
1sealed class Shape{
2 class Circle(var radius: Float): Shape()
3 class Square(var length: Int): Shape()
4 object Rectangle: Shape()
5 {
6 var length: Int = 0
7 var breadth : Int = 0
8 }
9}
10
11fun eval(e: Shape) =
12 when (e) {
13 is Shape.Circle -> println("Circle area is ${3.14*e.radius*e.radius}")
14 is Shape.Square -> println("Square area is ${e.length*e.length}")
15 Shape.Rectangle -> println("Rectangle area is ${Shape.Rectangle.length*Shape.Rectangle.breadth}")
16 }
这将结束Kotlin密封类教程。参考: Kotlin文档