Data Class
The data class is a special class in Kotlin, and its particularity is shown in the following aspects:
The primary constructor of the data class requires at least one parameter;
All parameters of the main constructor must be decorated with Val or var;
The data class cannot be abstract, open, sealed, or internal, meaning that the data class can only be final;
Before Kotlin 1.1, the data class could only implement interfaces and not inherit classes.
After Kotlin 1.1, the data class can inherit other classes
In the development process, we often need to use some classes, these classes are only used to store data, and no redundant functions. The data class in Kotlin exists for this purpose.
Here is a simple data class that represents a point in a two-dimensional plane:
Kotlin
Data class Point (Val x:double, Val y:double)
Since the data class requires at least one parameter (1th) for the primary constructor, and all parameters must be decorated with Val or Var (2nd), the use of the Val or var modifier parameter in the main constructor is a concise notation for Kotlin to create class properties, so The point class has the equivalent of two attribute x, Y.
When you call the point constructor, you must pass all the parameters x, Y. If you want to add a default constructor for the point class, you need to add a secondary constructor (secondary constructor):
Data class Point (Val x:double, Val y:double) {
constructor (): This (0.0, 0.0)
}
If you don't want to be so troublesome, the best way is to add a default value to the parameters of the main constructor:
Data class Point (Val x:double = 0.0, val y:double = 0.0)
copy Function-copy
As mentioned earlier, the data class is the class that holds it, then it is possible to copy the data when the data is manipulated, and the copy function copy is generated by default for the Kotlin. Simply calling the copy function will result in a new object that has the same property value as the original object:
Val Point = Point (1.0, 1.0)//(1.0, 1.0)
val point2 = point.copy () //(1.0, 1.0)
If you want to make some changes while copying, you can add parameter values directly to the copy function, using the form of a named parameter list:
Val Point = Point (1.0, 1.0) //(1.0, 1.0)
val point2 = point.copy () //(1.0, 1.0)
val point3 = point.copy ( y = 3.0) //(1.0, 3.0)
componentn () function
In addition to the copy function, Kotlin generates a COMPONENTN () function for the data class, where n represents the nth attribute of the data class. This makes it very convenient to deconstruct the data class object, and you can use (COM1, COM2) = DataObject To assign the two properties of DataObject to COM1 and COM2, respectively:
Val Point = Point (1.0, 3.0)
val (x, y) = point //x = 1.0, y = 3.0
The element type of the collection class Map in Kotlin is map.entry<k, v>, but the Kotlin itself class library uses the extension function (Extension) to Map.entry<k, V> adds Component1 (), The Component2 () function allows the map.entry<k to be deconstructed when traversing a Map, v>:
MAPOF (1 to "one"). ForEach {(key, value)-println ("$key-$value")}
data class in the Kotlin standard library
In the Kotlin class library has been provided for us to represent the two-tuple of the class Pair, representing the ternary group of Triple, their property names are first, second, third. It may be enough to use these two classes in most cases, but sometimes we need to write the data class ourselves to enhance the readability of the code by using the class and property names that are more consistent with the scenario.