3.14. Kotlin data class and sealed class

发布时间 : 2025-10-25 13:34:26 UTC      

Page Views: 10 views

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 } 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。