当前位置:网站首页>The first day of a solid foundation for Kotlin
The first day of a solid foundation for Kotlin
2022-08-08 20:33:00 【Do not forget the original intention z】
The first day of a solid kotlin foundation
Quickly query data in database classes, such as finding the largest number
First define a data class
data class Person(val name:String, val age:Int? =null) where?Indicates that this data can be empty, this is an important knowledge point that must be kept in mind!
Now use this database class to put
fun main(args:Array{
fun person = listOf(Person(“Zhang”),Person(“Zhao”,15))
}
Among them, we created a list collection, which stores two data classes. Note that the constructor of one of the data classes has only one name, because the second function we set when we created the data class can be empty., so if you don't enter a value, the system will create a variable with an age of null for us by default
Now compare the size of the two data classes age
val oldest =person.maxBy(it.age ?: 0) where ?: is the Elvis operator, which means that if there is no value, it will be replaced with 0, so Zhao is the maximum age
oldest is 15
Simple writing of functions, such as passing in two values to compare the maximum value
fun max(a : Int , b : Int) :Int {
return if(a>b) a else b
}
The above function is to compare the size of an Int value, but the function title of this function is composed of a single expressionIf so, you can use this expression as the complete function body, without the curly braces and return statement
fun max (a :Int , b : Int ) :Int = if (a > b) a else b If the function body is written inside curly braces, then the function has a code block, if it directly returns an expressionformula, then it is given to the body of the expression
About the difference between variable modifiers val var
Methods of declaring variables in java
String name = "Zhang San" It is necessary to declare that this variable is of type String, and if the variable needs to be immutable, a final constant needs to be added, but it is completely omitted in kotlin, just write
val name = "Zhang San" There is no need to apply for a variable type, because the kotlin compiler will deduce that this is a String type and add the val modifier, then this variable is immutable.However, one concept to understand is that the reference itself (name) is immutable, but the object it points to is mutable, such as pointing to a collection type, then the collection type is mutable, such as:
val list =arrayListOf("1")
list.add("5") This code is perfectly valid
var age = "5" A variable decorated with var is mutable, allowing to change its amount, butIt is not allowed to change its type; for example: var age = "5" | var age = "Zhangsan" This approach is wrong because its type is immutable.
Simpler string template
The output string in kotlin is println("Hello Kotlin") You can also use $(variable) in println, for example:
val name = "Kotlin" println("Hello ${name}") NoteThe point is that if there is no more character after $, you can remove the curly braces
Classes and Properties
To create a data class in java, we often need to create get and set methods for variables.But in Kotlin it is not needed.
class Person(val name:String, var age: Int) Kotlin will automatically create a get method for the name variable, because the name is val modified, then it has only one get method, but will create a get method for the var modified age variableand set method.
边栏推荐
- Questions about Mac terminal custom commands and Mysql command
- Maykel Studio OpenHarmony Device Development Training Notes - Chapter 6 Study Notes
- The WPF main form calls User32's SetWindowPos to set the form to the top, which will cause the problem of grabbing the focus with other forms
- OpenEuler's Ways to Improve Resource Utilization 02: Effects under Typical Applications
- 经验分享|低成本快节奏搭建企业知识管理系统的方法
- 1259 Alice and Bob
- WPF主窗体调用 User32的SetWindowPos 设置窗体置顶会导致与其他窗体抢夺焦点的问题
- 【翻译】用Argo CD揭开企业规模的持续交付的秘密成分
- idea 引入包报错:Unable to provision, see the following errors
- com.alibaba.fastjson.JSONException: default constructor not found. class
猜你喜欢
随机推荐
自定义MVC
Maykel Studio OpenHarmony Device Development Training Notes - Chapter 6 Study Notes
Web3到底是什么?
接口测试经典面试题:Session、cookie、token有什么区别?
Use fontforge to modify font, keep only numbers
正则表达式与文本处理器
JMeter测试接口并发场景
iMeta | 深圳先进院戴磊组开发可同时提取共存菌株的组成和基因成分谱的菌株分析工具...
leveldb-impl:level0
新库上线 | CnOpenDataA股上市公司基本信息数据
方舟开服务器教程——开服配置常见问题及解决方法
Matlab用回归、SEIRD模型、聚类预测美国总统大选、新冠疫情对中美经济的影响
西湖大学鞠峰组招聘【塑料降解 / 污水工程 / 微生物学】方向博士后和科研助理...
方舟综合指令代码大全系统综合
PHPUnit 单元测试
学习笔记:栈的应用1_递归(重点)
莫让“学院派”限制我们的思维:在数组的中间位置删除数据一定比链表慢?
nacos作用
方舟基础物品指令代码大全
阿里云OSS文件下载到本地指定文件有坑