Kotlin(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Aaron_Gan/article/details/79206522

Kotlin(一)

写在前面:

  • 参考资料:

《Kotlin官方文档》《Kotlin for Android 开源中文版》

  • 准备工作:

Android-Studio2.x:添加Kotlin扩展插件,3.x默认支持Kotlin,开箱既得
添加项目依赖(app下的build gradle)

exclude group: 'com.android.support', module: 'support-annotations'
})
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile('com.github.VictorChow:KotlinAndroidLib:0.1.0') {
    transitive = false
}
compile "org.jetbrains.anko:anko-sdk15:0.9.1" // So here it's 15 too
compile "org.jetbrains.anko:anko-appcompat-v7:0.9.1"
compile "org.jetbrains.anko:anko-design:0.9.1"
compile "org.jetbrains.anko:anko-recyclerview-v7:0.9.1"   

关于Anko库:Anko是JetBrains开发的一个强大的库。它主要的目的是用来替代以前XML的方式来使用代码生成UI布局:例如Toast:toast(“This is a Toast”)即可

基础语法:

基本语法

数据类型及关键字参见官方文档及资料
val:局部常量:必须在定义的时候赋初始值
var:局部变量
静态变量或常量以及方法:类比java中的static关键字

companion object:伴生对象(或者理解为陪伴类一生的对象?)
在companion object {
    //静态变量
        private var name: String?= null
       //静态方法
        fun initName(name1:String?): String?{
            this.name = name1
    }
}

量的定义:

1.lateinit var name:String(lateinit var: kotlin软关键字:此处表示对变量在后期再进行初始化工作)
2.var name:String = "xxx"(这种定义方式必须赋值,且不能为null)
3.var name:String? = null (类比上一种,可以赋值怒null,注意?这个符号,这是Kotlin语言的一个重要符号,可以理解为:允许为空值,后面还会遇到)
4.val name:String = "xxx"(val关键字修饰的常量,必须在定义时初始化)

方法定义或其他

1.fun method(参数:参数类型):(返回值:返回值类型){方法体}
2.override fun onCreate(参数表){}      //override重写关键字,此处即重写onCreate(参数表)方法
3.静态方法:见上文

类型转换

1.toString()等方法
2.as关键字:Java中:textView:TextView = (TextView)findViewbyId(R.id.xxx)
            Kotlin中:textView = findViewbyId(R.id.xxx) as TextView
            或者有使用Anko:textView = find(R.id.xxx)as TextView

类的继承和接口的实现

A,B为类,C为接口

java:
class A extends B implements C

kotlin:
class A: B , C

构造方法

1.默认构造方法:

class myAdapter(val context: Context,var isCollect:Boolean): RecyclerView.Adapter<MyViewHolder>() {}
主构造方法(或者称为默认构造方法)紧跟在类名后面(参数表中定义的变量或者常量,类似于java中在类中的普通变量或者常量,无需在类中再次定义)。
或者:
class myAdapter: RecyclerView.Adapter<MyViewHolder>() {
//这是第二种写带参主构造方法的写法
    constructor(val context: Context) {}
//这是第三种写带参主构造方法的写法
    constructor(val context: Context):super(context){}
}


2.次构造方法:

class myAdapter(val context: Context,var isCollect:Boolean): RecyclerView.Adapter<MyViewHolder>() {

    lateinit var storiesList: List<ThemesItemsBean.StoriesBean>

    constructor(context: Context , themesItemsBean: ThemesItemsBean,isCollect: Boolean) : this(context,isCollect){
        storiesList = themesItemsBean.stories
    }
次构造方法:使用constructor(参数表)this(默认参数表(必须要有)){方法体}
}

构造方法注意点:constructor软关键字可声明主次构造方法,但是主构造方法不能加(:this(默认参数表),主构造方法可后接:super(参数)),次构造方法不能少(:this(默认参数表))

实际代码中遇到的一些坑

1.添加和切换fragment:

java中:
transaction = FragmentManager.beginTransaction()
再通过这个transaction来实现添加或者隐藏显示fragment
kotlin中:
val transaction: android.support.v4.app.FragmentTransaction = supportFragmentManager.beginTransaction()
注意这里的supportFragmentManager虽然方法中也有提示Fragment,但在实际使用中,会出现参数表不匹配的错误导致无法添加成功

2.Actionbar的使用:

在java中:
    setSupportActionBar(ToolBar)
    ActionBar actionBar = getSupportActionBar()
    actionBar.setTitle("xxxxxx")
在kotlin中:
   setSupportActionBar(ToolBar)
   supportActionBar?.setTitle("xxxxxx")

3.Anko的find方法在ViewHolder类中使用报错:

貌似是null can not cast to non-null:

原来的使用方法是:
var textView = itemView.find(R.id.xxx) as TextView
解决办法(查找自StackOverFlow):
var textView = itemView.findViewById(R.id.xxx) as? TextView

4.关于List< ClassType>

不同于java,kotlin语言一大优势就在于更好的处理null异常,而这里的List<>,在kotlin中是没有提供诸如add,set等方法的,只是默认提供了get和其他一些只读操作方法

var storiesList:List<StoriesBean> = ArrayList<StoriesBean>()
这种写法无法在代码中使用storiesList.add(xxx)(不会出现add()方法)
//解决办法:
var storiesList:MutableList<StoriesBean> = ArrayList<StoriesBean>()
storiesList.add(xxx)(这样才能正常add或者set)

猜你喜欢

转载自blog.csdn.net/Aaron_Gan/article/details/79206522