kotlin语法杂记

2019-01-24  本文已影响0人  在一颗大大大榕树下

将使用kotlin过程中出现的问题记录下来

条件表达式

//java 
int c = a > b ? a:b
//kotlin
var c = if(a > b)  a else b 

类型检查

//kotlin
i is Int

i !is Int

表达Class

//java
Intent intent = new Intent(this,MyService.class);

//kotlin
val intent = Intent(this,MyService::class.java)

序列化

  1. 1.1.4版本以上,app module中:apply plugin: 'kotlin-android-extensions'
  2. app module: android中添加
  androidExtensions {
        experimental = true
   }
  1. 使用注解,就能自动生成序列化的代码了
@Parcelize
data class Account(val mUsername:String,val mPassword:String) : Parcelable

bean类

   data class Person(var username:String,var password:String){
        override fun toString(): String {
            return "Person{username=$username,password=$password}"
        }
    }

单例模式
Kotlin下的5种单例模式:https://www.jianshu.com/p/5797b3d0ebd0
赞美作者,写的很棒。
还有一种可以使用 by by Delegates.notNull()代理

泛型java:Class<?> -->kotlin

//java
Class<?>

//kotlin

Class<*>

kotlin自定义注解

//java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @Interface ClassId{
    String value();
}

//kotlin
@Target(AnnotationTarget.TYPE)
@Retention(AnnotationRetention.RUNTIME)
annotation class ClassId(val value:String)

可变参数组 vararg

fun <T> asList(vararg ts: T): List<T> {
    val result = ArrayList<T>()
    for (t in ts) // ts is an Array
        result.add(t)
    return result
}

//允许将可变数量的参数传递给函数
val list = asList(1, 2, 3)

until和in的区别

for (i in 1..5) {   // for (int i = 1;i <= 5;i++) {print(i)}
  print(i)  // 12345
}

for (i in 1 until 5) {
  print(i) // 1234
}

内联函数
inline

const关键词
基本等同于 final static

get方法

 private val isViewAttached: Boolean
        get() = mRootView != null

其实也算是一种赋值,isViewAttached的值就由判断语句mRootView != null来决定

kotlin写Handler

        val handler = Handler()
        handler.postDelayed({skipToHome()},1800)

不用写Runnable直接套跳转方法是不是很nice

@JvmOverloads 注解

在有默认参数值的方法中使用@JvmOverloads注解,则Kotlin就会暴露多个重载方法。

什么意思,直接看代码,一个典型的使用场景就是自定义控件了

public class CustomView extends LinearLayout {
    public CustomView (Context context) {
        this(context, null);
    }

    public CustomView (Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
}

kotlin使用注解

class BottomBar @JvmOverloads constructor(context: Context, 
                                          attrs: AttributeSet? = null, 
                                          defStyleAttr: Int = 0) :
    LinearLayout(context, attrs, defStyleAttr) {
    init{
      init(context, attrs);
    }
}

@JvmStatic 和 @JvmField 注解

class AnnotationTest{
    companion object {
        @JvmStatic
        var name:String = ""
    }
}

可以看到nameAnnotationTest中的一个静态对象
转换成java代码,name是一个私有的成员变量,并且自动生成的get/set方法

如果替换成@JvmField
这里name就会变为public修饰,并且不会生成get/set方法

JNI中kotlin的native方法
java中使用native
kotlin中使用external

   private external fun openFile():Long 

@Volatile
替代了java中的volatile
会做修饰词类型的判定,注意修饰变量var ,不可修饰immutable参数

位运算的表达方式

位运算的效率很高,这也是大量源码中%或者&被位运算取代的原因,在kotlin中位运算不能用<<表示

//java
public static final int result= 1 << 11;
//kotlin
val result = 1 shl 11

关于静态内部类

//java
inner class  A{
    static class  B{

    }  
}
//kotlin
class A{
    class B{

    }
}
上一篇下一篇

猜你喜欢

热点阅读