当前位置:网站首页>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
边栏推荐
- MySQL notes 2_ data sheet
- 取消远程依赖,用本地依赖
- AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
- 第三篇:docker安装mysql容器(自定义端口)
- js时间获取本周一、周日,判断时间是今天,今天前、后
- [多屏互动] 实现双多屏异显二:startActivity方式
- 统一任务分发调度执行框架
- 红外传感器控制开关
- BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题
- Android面试计网面经大全【持续更新中。。。】
猜你喜欢

C connection of new world Internet of things cloud platform (simple understanding version)

Google AdMob advertising learning

个人博客网站搭建

Ffmpeg common commands

AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi

What did you do during the internship

组件化学习(3)ARouter中的Path和Group注解

常用UI控件简写名

this. getOptions is not a function

iTOP4412 HDMI显示(4.0.3_r1)
随机推荐
取消远程依赖,用本地依赖
ViewPager2实现画廊效果执行notifyDataSetChanged后PageTransformer显示异常 界面变形问题
Kotlin征途之data class [数据类]
三子棋小游戏
Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
Itop4412 HDMI display (4.4.4_r1)
WebRTC ICE candidate里面的raddr和rport表示什么?
[sm8150] [pixel4] LCD driver
MySQL notes 5_ Operation data
【2021年新书推荐】Professional Azure SQL Managed Database Administration
Fill the network gap
What did you do during the internship
[recommendation of new books in 2021] enterprise application development with C 9 and NET 5
iTOP4412 SurfaceFlinger(4.4.4_r1)
iTOP4412 LCD背光驱动(PWM)
[recommendation for new books in 2021] professional azure SQL managed database administration
adb shell常用模拟按键keycode
MySQL笔记4_主键自增长(auto_increment)
发布自定义插件到本地服务器
this. getOptions is not a function