Kotlin data class and sealed class


Release date:2023-09-22 Update date:2023-10-13 Editor:admin View counts:362

Label:

Kotlin data class and sealed class

Data class

Kotlin can create a class that contains only data with the keyword data :

data class User(val name: String, val age: Int)

The compiler automatically extracts the following functions from the main constructor based on all declared attributes:

  • equals() / hashCode()

  • toString() format such as "User(name=John, age=42)"

  • componentN() functions corresponding to attributes, in order of declaration

  • copy() function

If these functions are explicitly defined in the class or inherited from thesuperclass, they will no longer be generated.

In order to ensure that the generated code is consistent and meaningful, thedata class needs to meet the following conditions:

  • The main constructor contains at least one parameter.

  • All parameters of the main constructor must be identified as val or var ;

  • Data classes cannot be declared as abstract , open , sealed , or inner;

  • Data classes cannot inherit from other classes (but can implement interfaces).

Copy

Copy and use copy() function, which we can use to copy the object and modify some properties. For the User class above, the implementation will besimilar to the following:

fun copy(name: String = this.name, age: Int = this.age) = User(name, age)

Example

Use copy class replication User data class, and modify age attributes:

data class User(val name: String, val age: Int)


fun main(args: Array<String>) {
    val jack = User(name = "Jack", age = 1)
    val olderJack = jack.copy(age = 2)
    println(jack)
    println(olderJack)

}

The output is as follows:

User(name=Jack, age=1)
User(name=Jack, age=2)

Data classes and deconstructing declarations

Component functions allow data classes to be used in deconstructing declarations:

val jane = User("Jane", 35)
val (name, age) = jane
println("$name, $age years of age") // prints "Jane, 35 years of age"

Standard data class

The standard library provides Pair and Triple . In most cases, named data classes are a better design choice because the code is more readable and provides meaningful names and attributes.

Sealed class

Sealed classes are used to represent restricted class inheritance structures: when a value is limited to a limited number of types and there cannot be any other types. In a sense, they are extensions of enumerated classes: the set of values of enumerated types is also limited, but there isonly one instance of each enumeration constant, and a subclass of a sealed class can have multiple instances of states that can be contained.

Declare a sealed class, using the sealed a modified class, a sealed class can have subclasses, but all subclasses must be embedded in the sealedclass.

sealed cannot modify interface , abstract class (warning will be reported, but compilation errors will not occur)

sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()

fun eval(expr: Expr): Double = when (expr) {
    is Const -> expr.number
    is Sum -> eval(expr.e1) + eval(expr.e2)
    NotANumber -> Double.NaN
}

The key benefit of using a sealed class is to use the when in the case of an expression, if you can verify that the statement covers all cases, youdo not need to add another else clause.

fun eval(expr: Expr): Double = when(expr) {
    is Expr.Const -> expr.number
    is Expr.Sum -> eval(expr.e1) + eval(expr.e2)
    Expr.NotANumber -> Double.NaN
    // The `else` clause is no longer needed because we have covered
 all cases
}

Powered by TorCMS (https://github.com/bukun/TorCMS).