文章目录
修饰符
kotlin和Java修饰符比较
java修饰符 | 含义 | kotlin修饰符 | 含义 |
---|---|---|---|
public | 对所有类可见 | public | 对所有类可见,在kotlin中是默认项 |
private | 只对当前类内部可见 | private | 只对当前类内部可见 |
protected | 对当前类、子类和统一包路径下的类可见 | protected | 对当前类、子类和统一包路径下的类可见 |
default | 默认修饰项 | internal | 只对同一模块中的类可见 |
数据类
Java创建数据类的方法
public class Cellphone {
String brand;
double price;
public Cellphone(String brand, double price) {
this.brand = brand;
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cellphone cellphone = (Cellphone) o;
return Double.compare(cellphone.price, price) == 0 &&
Objects.equals(brand, cellphone.brand);
}
@Override
public int hashCode() {
return Objects.hash(brand, price);
}
@Override
public String toString() {
return "Cellphone{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
kotlin创建数据类
data class Cellphone2(val brand:String,val price:Double)
单例
Java创建一个单例
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public synchronized static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void singletonTest() {
System.out.println("singletonTest is called");
}
}
kotlin创建一个单利(只需要将class关键字改为object)
object Singleton2 {
fun singletonTest(){
println("singletonTest is called")
}
}
kotlin集合的创建与遍历
List
创建一个不可变的集合(该集合只能用于读取,无法对集合进行添加、修改或删除操作)
val list2= listOf("Apple","Banana","Orange","Pear","Grape")
for (fruit in list2){
println(fruit)
}
打印日志如下:
Apple
Banana
Orange
Pear
Grape
Process finished with exit code 0
创建一个可变的集合
val list = mutableListOf("Apple", "Banana", "Orange", "Pear", "Grape")
list.add("Watermelon")
for (fruit in list){
println(fruit)
}
打印日志如下:
Apple
Banana
Orange
Pear
Grape
Watermelon
Process finished with exit code 0
Set
Set集合与list集合一样,对应的不可变集合与可变集合分别为setOf()和mutableSetOf(),注意Set集合是无序的
Map集合
仿Java的方法添加数据并遍历
val map=HashMap<String,Int>()
map.put("Apple",1)
map.put("Banana",2)
map.put("Orange",3)
map.put("Pear",4)
map.put("Grape",5)
for ((fruit,number) in map){
println("fruit is "+fruit+",number is "+number)
}
打印结果:
fruit is Apple,number is 1
fruit is Pear,number is 4
fruit is Grape,number is 5
fruit is Orange,number is 3
fruit is Banana,number is 2
Process finished with exit code 0
kotlin的写法:
val map=HashMap<String,Int>()
map["Apple"]=1
map["Banana"]=2
map["Orange"]=3
map["Pear"]=4
map["Grape"]=5
for ((fruit,number)in map){
println("fruit is "+fruit+",number is "+number)
}
打印结果如下:
fruit is Apple,number is 1
fruit is Pear,number is 4
fruit is Grape,number is 5
fruit is Orange,number is 3
fruit is Banana,number is 2
Process finished with exit code 0
进一步简化写法:
val map= mapOf("Apple" to 1,"Banana" to 2,"Orange" to 3,"Pear" to 4,"Grape" to 5)
集合的函数式API
问题:如何在一个集合中找到单词最长的那个水果?
普通写法:
val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
var maxlengthFruit=""
for (fruit in list){
if (fruit.length>maxlengthFruit.length){
maxlengthFruit=fruit
}
}
println("maxlengthFruit is "+maxlengthFruit)
集合的函数式API写法
val list= listOf("Apple","Orange","Pear","Grape","Watermelon")
val maxLengthFruit= list.maxByOrNull { it.length }
println("maxlengthFruit is "+maxLengthFruit)
空指针检查
kotlin将空指针检查提前到了编译时期,所有参数和变量都不可为空,但是如果业务需要某个参数或者变量为空,就是在类名后面加上一个?,比如Int表示不可为空的整形,而Int?表示可以为空的整形
判空辅助工具
?.
操作符
例如这段判空代码
if(a!=null){
a.soSomething()
}
用?.
操作符就可以简化成
a?.doSomething()
?:
操作符
这个操作符的左右两边都接收一个表达式,如果左边表达式的结果不为空就返回左边表达式的结果,否则就返回右边表达式的结果
先看看普通写法:
val c=if(a!=null){
a
}else{
b
}
用?:
写法则为
val c=a?:b
?.
和?:
结合使用
例如我门要编写一个函数用来获取一段文本的长度,传统写法为:
fun getTextLength(text:String):Int{
if(text!=null){
return text.length
}
return 0
}
?.
和?:
结合使用写法
fun getTextLength(text:String)=text?.length?:0
!!.
非空断言
fun printUpperCase(){
val upperCase = content!!.toUpperCase()
println(upperCase)
}
如上例子,表示我确信content不为空,所以不用来帮我进行空指针检查了
let函数
obj.let{obj2 ->
//编写具体的业务逻辑
}
字符串内嵌表达式
语法规则:
"helllo, ${obj.name}. nice to meet you"
当表达式中仅仅有一个变量的时候,还可以将两边大括号省略,如下所示:
hello $name . nice to meet you
函数的参数默认值
fun printParams(num:Int,str:String="hello"){
println("num is $num, str is $str")
}
如上,给str参数默人一个“hello”的默认值,在main方法中调用这个函数如下:
fun main(){
printParams(num="123")
}
打印结果如下:
num is 123, str is hello
Process finished with exit code 0