当前位置:网站首页>Data class of kotlin journey
Data class of kotlin journey
2022-04-23 07:16:00 【Lance_ w】
Kotlin And data class [ Data class ]
kotlin The normal way to declare a class class [ Class name ], And java similar
kotlin The declaration of a class can also use data Keyword to mark classes that are only used to hold data , Call it a data class
eg:
data class User constructor(val name: String) {
var nickName: String = ""
}
For example, above user Class is a declared data class
The declaration of data class must meet the following requirements :
-
The main constructor needs to have at least one parameter
-
All parameters of the main constructor must be marked val perhaps var
-
Data classes cannot be abstract 、 Development 、 Sealed or internal
Why? data class There must be the above constraints ?
Let's take a look at a common statement class class , What is the difference between meeting the above requirements and not meeting the above requirements
```kotlin
class User constructor(name: String) {
}
/** Aforementioned user class , Parameters in the main constructor name Is not to bring val、var * It means that we should name Just pass in parameters as constructors , This variable can only be used in init Get... In initialization * And if necessary name Declared as a class variable , You need to make the following changes */
class User constructor(name1: String) {
var name: String = ""
init {
name = name1
}
}
// The above member variable declaration can be completed on the main constructor , namely
class User constructor(var name: String) {
}
Therefore, the above restrictions require that the data class declare the contained member variables in the main constructor , Then the compiler generates some specific functions for us through these declared member variables .
Official documents also point out that :
about data class The compiler automatically exports the following members from all properties declared in the main constructor :
equals()
/hashCode()
Yes ;toString()
The format is"User(name=John, age=42)"
;componentN()
function Corresponds to all properties in the declared order ;copy()
function
That is to say data Generated equals、hashCode、toString Methods are implemented according to the member variables declared in the main constructor .
Here's an example :
// Declare the following data class
data class User constructor(var name: String) {
var nickName: String = ""
}
val user1 = User("Lance")
user1.nickName = "ming"
val user2 = User("Lance")
user2.nickName = "ning"
user1==user2 because equals、hashCode Are all based on name, Which is assigned here Lance To compare
meanwhile user1、user2 Of toString Method returns User(name=Lance)
If you modify the above code by
data class User constructor(var name: String, var nickName: String) {
}
val user1 = User("Lance", "ming")
val user2 = User("Lance", "ning")
user1!=user2 because equals、hashCode Are all based on name and nickName To compare
meanwhile user1、user2 Of toString Method returns User(name=Lance,nickName=ming)、User(name=Lance,nickName=ning)
版权声明
本文为[Lance_ w]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230608223711.html
边栏推荐
- 树莓派:双色LED灯实验
- BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView 滑动问题
- MySQL notes 5_ Operation data
- Tiny4412 HDMI显示
- Miscellaneous learning
- Android interview Online Economic encyclopedia [constantly updating...]
- Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
- Itop4412 surfaceflinger (4.4.4_r1)
- JNI中使用open打开文件是返回-1问题
- [2021 book recommendation] learn winui 3.0
猜你喜欢
随机推荐
树莓派:双色LED灯实验
常见的正则表达式
JNI中使用open打开文件是返回-1问题
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
MySQL笔记5_操作数据
[2021 book recommendation] kubernetes in production best practices
[recommendation for new books in 2021] professional azure SQL managed database administration
Encapsulate a set of project network request framework from 0
去掉状态栏
winform滚动条美化
组件化学习
adb shell 常用命令
补补网络缺口
Miscellaneous learning
组件化学习(2)Arouter原理学习
DCMTK (dcm4che) works together with dicoogle
Using queue to realize stack
“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
第三篇:docker安装mysql容器(自定义端口)
Project, how to package