Kotlin从入门到放弃KotlinKotlin学习日记

【Kotlin学习日记】Day13:数据类

2017-07-03  本文已影响101人  William李梓峰

大家好,我是William李梓峰,欢迎加入我的Kotlin学习之旅。
今天是我学习 Kotlin 的第十三天,内容是 Data Classes - 数据类。

官方文档:

Data Classes - 数据类

We frequently create a class to do nothing but hold data. In such a class some standard functionality is often mechanically derivable from the data. In Kotlin, this is called a data class and is marked as data:
我们经常创建一个类却仅仅是为了装载数据。这种(万恶的 DTO)类的唯一功能就是传递数据而已。在 Kotlin 里面,这种类就叫数据类,并用 data 标识:

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

// 我更喜欢这样:
data class User(
    val name: String,
    val age: Int
)

The compiler automatically derives the following members from all properties declared in the primary constructor:
编译器自动地处理所有在主要构造器上声明的形参,生成对应的属性,同时还会生成一些预设方法:

包括

If any of these functions is explicitly defined in the class body or inherited from the base types, it will not be generated.
如果上述这些任何一个函数显式地定义在一个类体中或从其他父类继承过来,它则不会自动生成了。(所以没事就别继承了,也别手贱自己去定义,用默认的挺好的)

To ensure consistency and meaningful behavior of the generated code, data classes have to fulfil the following requirements:
为了确保数据一致性和生成有意义的代码,数据类必须满足以下要求:

Since 1.1, data classes may extend other classes (see Sealed classes for examples).
自从 1.1 版本以来,数据类都可以 extend(继承或扩展) 其他类。(例如,密封类,又跳了)

On the JVM, if the generated class needs to have a parameterless constructor, default values for all properties have to be specified
(see Constructors).
在 JVM 里面,如果已经生成的类需要一个无参构造器,那么所有属性的默认都需要被指定(请看 构造器,跳回去了,如果所有字段都有默认值,编译器会自动生成一个无参构造器)

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

Copying - 复制

It's often the case that we need to copy an object altering some of its properties, but keeping the rest unchanged.
我们经常需要复制某个对象的部分字段。而另外一部分字段不需要改变。

This is what copy() function is generated for. For the User class above, its implementation would be as follows:
copy() 方法为此而生。例如上面的 User 类,就实现了一个:

// 系统默认生成一个 copy()
fun copy(name: String = this.name, age: Int = this.age) = User(name, age)     

This allows us to write
我们可以这样写

val jack = User(name = "Jack", age = 1)
val olderJack = jack.copy(age = 2) // 直接改变 age 值,而 name 不变

Data Classes and Destructuring Declarations - 数据类与解构声明

Component functions generated for data classes enable their use in destructuring declarations:
Component 方法 生成于数据类中,相当于直接让其能够做 解构声明(又跳了)

val jane = User("Jane", 35) 
val (name, age) = jane  // 这里做了解构声明,有点像 bash shell 参数展开
println("$name, $age years of age") // prints "Jane, 35 years of age"

Standard Data Classes - 标准的数据类

The standard library provides Pair and Triple. In most cases, though, named data classes are a better design choice, because they make the code more readable by providing meaningful names for properties.
标准库提供 Pair(一对) 和 Triple(三胞胎)。大多数情况下,即便如此,数据类的命名还是一个更好的设计选择,因为它们能够让代码可读性更高一些。(Pair 和 Triple 都是 Kotlin 预设的类,方便数据装载,这里官方更推荐自己写)

上一篇下一篇

猜你喜欢

热点阅读