当前位置:网站首页>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
边栏推荐
- face_recognition人脸检测
- 组件化学习(1)思想及实现方式
- C connection of new world Internet of things cloud platform (simple understanding version)
- Binder mechanism principle
- Ffmpeg common commands
- webView因证书问题显示一片空白
- WebRTC ICE candidate里面的raddr和rport表示什么?
- [2021 book recommendation] artistic intelligence for IOT Cookbook
- 给女朋友写个微信双开小工具
- 从0开始封装一套项目的网络请求框架
猜你喜欢

Record WebView shows another empty pit

PaddleOCR 图片文字提取
![[2021 book recommendation] artistic intelligence for IOT Cookbook](/img/8a/3ff45a911becb895e6dd9e061ac252.png)
[2021 book recommendation] artistic intelligence for IOT Cookbook

杂七杂八的学习

What did you do during the internship
![[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide](/img/36/1c484aec5efbac8ae49851844b7946.png)
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide

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

webView因证书问题显示一片空白

机器学习笔记 一:学习思路

组件化学习(3)ARouter中的Path和Group注解
随机推荐
MySQL notes 2_ data sheet
MySQL notes 1_ database
【2021年新书推荐】Learn WinUI 3.0
MySQL笔记5_操作数据
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
Antd Design Form表单检验
What did you do during the internship
ffmpeg常用命令
SSL/TLS应用示例
[2021 book recommendation] red hat rhcsa 8 cert Guide: ex200
Android room database quick start
iTOP4412 SurfaceFlinger(4.0.3_r1)
Oracle Job定时任务的使用详解
launcher隐藏不需要显示的app icon
【2021年新书推荐】Practical IoT Hacking
Ffmpeg common commands
[sm8150] [pixel4] LCD driver
素数求解的n种境界
sys.dbms_scheduler.create_job创建定时任务(功能更强大丰富)
给女朋友写个微信双开小工具