3.14. Kotlin data class and sealed class

发布时间 :2023-10-12 23:00:04 UTC      

3.14.1. 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).

3.14.2. 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)

3.14.3. 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)

3.14.4. 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"

3.14.5. 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.

3.14.6. 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
}

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.