Kotlin 简明教程
Kotlin - Data Classes
在此章节中,我们将学习 Kotlin 数据类。Kotlin 数据类被用来仅保存数据,除了保存数据之外,它没有任何其他功能。
In this chapter, we will learn about Kotlin Data Classes. A Kotlin Data Class is used to hold the data only and it does not provide any other functionality apart from holding data.
Kotlin 类被定义为数据类需满足以下条件:
There are following conditions for a Kotlin class to be defined as a Data Class:
-
The primary constructor needs to have at least one parameter.
-
All primary constructor parameters need to be marked as val or var.
-
Data classes cannot be abstract, open, sealed, or inner.
-
The class may extend other classes or implement interfaces. If you are using Kotlin version before 1.1, the class can only implement interfaces.
Syntax
定义 Kotlin 数据类很简单。如果用 data 关键字标记了 Kotlin 类,则它就变成了数据类。例如:
It’s simple to define a Kotlin Data Class. If a Kotlin class is marked with data keyword then it becomes a data class. For example:
data class Book(val name: String, val publisher: String, var reviewScore: Int)
Kotlin 数据类的好处在于,当你声明一个 Kotlin 数据类时,编译器将自动生成构造器、toString()、equals()、hashCode() 和额外的 copy() 以及 componentN() 函数。
Good thing about Kotlin Data Class is that when you declare a Kotlin Data Class, the compiler generates Constructor, toString(), equals(), hashCode(), and additional copy() and componentN() functions automatically.
Example
Kotlin 数据类的实例化方法与其他 Kotlin 类相同:
A Kotlin Data Class is instantiated the same way as other Kotlin classes:
data class Book(val name: String, val publisher: String, var reviewScore: Int)
fun main(args: Array<String>) {
val book = Book("Kotlin", "Tutorials Point", 10)
println("Name = ${book.name}")
println("Publisher = ${book.publisher}")
println("Score = ${book.reviewScore}")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Name = Kotlin
Publisher = Tutorials Point
Score = 10
Copy Function
当我们定义一个 Kotlin 数据类时,copy() 函数将自动创建。此 copy 函数可用于复制一个对象,更改其一些属性,但保持其他属性不变。以下是一个示例:
The copy() function is created automatically when we define a Kotlin Data Class. This copy function can be used to copy an object altering some of its properties but keeping the rest unchanged. Following is an example:
data class Book(val name: String, val publisher: String, var reviewScore: Int)
fun main(args: Array<String>) {
val original = Book("Kotlin", "Tutorials Point", 10)
val copied = original.copy(reviewScore=5)
println("Original Book")
println("Name = ${original.name}")
println("Publisher = ${original.publisher}")
println("Score = ${original.reviewScore}")
println("Copied Book")
println("Name = ${copied.name}")
println("Publisher = ${copied.publisher}")
println("Score = ${copied.reviewScore}")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Original Book
Name = Kotlin
Publisher = Tutorials Point
Score = 10
Copied Book
Name = Kotlin
Publisher = Tutorials Point
Score = 5
toString Function
当我们定义一个 Kotlin 数据类时,toString() 函数也会自动创建。此函数返回对象的字符串表示。以下是一个示例:
The toString() function is also created automatically when we define a Kotlin Data Class. This function returns a string representation of the object. Following is an example:
data class Book(val name: String, val publisher: String, var reviewScore: Int)
fun main(args: Array<String>) {
val book = Book("Kotlin", "Tutorials Point", 10)
println(book.toString())
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Book(name=Kotlin, publisher=Tutorials Point, reviewScore=10)
hashCode() and equals() Functions
hasCode()函数返回对象的哈希值。如果两个对象相等,则 hashCode() 返回对象的相同整数值。
The hasCode()function returns hash code for the object. If two objects are equal, hashCode() returns the same integer value for the objects.
equals() 函数返回 true (如果两个对象相等)或 false (如果两个对象不相等)。
The equals() function returns true if two objects are equal or they have same hasCode value otherwise it returns false.
下面是一个示例:
Following is an example:
data class Book(val name: String, val publisher: String, var reviewScore: Int)
fun main(args: Array<String>) {
val original = Book("Kotlin", "Tutorials Point", 10)
val copy1 = original.copy(reviewScore=5)
val copy2 = original.copy(reviewScore=7)
println("Original Hashcode = ${original.hashCode()}")
println("Copy1 Hashcode = ${copy1.hashCode()}")
println("Copy2 Hashcode = ${copy2.hashCode()}")
if( copy1.equals(copy2)){
println("Copy1 is equal to Copy2.")
}else{
println("Copy1 is not equal to Copy2.")
}
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Original Hashcode = 1957710662
Copy1 Hashcode = 1957710657
Copy2 Hashcode = 1957710659
Copy1 is not equal to Copy2.
Destructuring Declarations
我们可以使用解构声明将一个对象解构为多个变量。例如:
We can destructure an object into a number of variables using destructing declaration. For example:
data class Book(val name: String, val publisher: String, var reviewScore: Int)
fun main(args: Array<String>) {
val book = Book("Kotlin", "Tutorials Point", 10)
val( name, publisher,reviewScore ) = book
println("Name = $name")
println("Publisher = $publisher")
println("Score = $reviewScore")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Name = Kotlin
Publisher = Tutorials Point
Score = 10