当前位置:网站首页>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灯实验
- org.xml.sax.SAXParseException; lineNumber: 141; columnNumber: 252; cvc-complex-type.2.4.a: 发现了以元素 ‘b
- Handler进阶之sendMessage原理探索
- iTOP4412无法显示开机动画(4.0.3_r1)
- MySQL笔记1_数据库
- 窗口分析函数LAST_VALUE,FIRST_VALUE,lag,lead
- 【2021年新书推荐】Learn WinUI 3.0
- org. xml. sax. SAXParseException; lineNumber: 141; columnNumber: 252; cvc-complex-type. 2.4. a: Found element 'B
- Binder mechanism principle
猜你喜欢

【2021年新书推荐】Artificial Intelligence for IoT Cookbook

WebView displays a blank due to a certificate problem

【2021年新书推荐】Learn WinUI 3.0

组件化学习(1)思想及实现方式

微信小程序 使用wxml2canvas插件生成图片部分问题记录

./gradlew: Permission denied

【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
![[2021 book recommendation] learn winui 3.0](/img/1c/ca7e05946613e9eb2b8c24d121c2e1.png)
[2021 book recommendation] learn winui 3.0
![[2021 book recommendation] practical node red programming](/img/f4/e397c01f1551cd6c59ea4f54c197e6.png)
[2021 book recommendation] practical node red programming

Android清除应用缓存
随机推荐
[2021 book recommendation] artistic intelligence for IOT Cookbook
【2021年新书推荐】Enterprise Application Development with C# 9 and .NET 5
MySQL笔记2_数据表
iTOP4412 FramebufferNativeWindow(4.0.3_r1)
oracle视图相关
MySQL笔记3_约束_主键约束
iTOP4412 SurfaceFlinger(4.0.3_r1)
MySQL5.7插入中文数据,报错:`Incorrect string value: ‘\xB8\xDF\xAE\xF9\x80 at row 1`
利用栈实现队列的出队入队
Binder机制原理
AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
[2021 book recommendation] effortless app development with Oracle visual builder
DCMTK(DCM4CHE)与DICOOGLE协同工作
Bottom navigation bar based on bottomnavigationview
cmder中文乱码问题
xcode 编译速度慢的解决办法
一款png生成webp,gif, apng,同时支持webp,gif, apng转化的工具iSparta
微信小程序 使用wxml2canvas插件生成图片部分问题记录
电脑关机程序
Itop4412 kernel restarts repeatedly