Kotlin基础语法总结,快速熟悉,上手开发

先熟悉一下基本数据类型

类型位宽度
Double64
Float32
Long64
Int32
Short16
Byte8

1、类和属性的定义

/**
 * 类的定义及属性
 */
open class A {
    lateinit var name : String
    var age : Int = 0
    var gender : Int = 0

    fun setInterface(i : I){

    }
}

    //定义int的方式一
    var a : Int = 0
    //定义int的方式二,同一
    var b = 0
    //定义一个字符串
    var s = "ssssss"
    //定义String,lateinit关键字 可以不初始化,但在使用前必须初始化,否则会空指针
    lateinit var c : String
    //加个问号,表名这个属性可以为空,在使用时需要在属性名后加双感叹号,表示属性不为空时调用方法,   如:d!!.toString()
    var d : String? = null

2、方法定义

    /**
     * 无参方法定义
     */
    fun main(){

    }

    /**
     * 有参方法定义
     * args String数组定义
     */
    fun main(args: Array<String>) {

    }
    /**
     * 有返回值方法定义
     * 返回字符串
     */
    fun mains(): String {

        return ""
    }

     /**
     * 有默认参数方法的定义
     */
    fun print(s : String = "没有内容"){
        
    }

    //调用
        //不传参数,则s = "没有内容"
        print()
        //传参,则s = "我是内容"
        print("我是内容")

3、部分具体语法

        //list基本语法
        val list = ArrayList<String>()
        list.add("")
        val s = list[0]
        val size = list.size
        //list遍历
        for(i in list.indices){
            val s = list[i]
        }
        list.forEach {
            val s = it
        }

        //普通循环
        for(i in 0..9){

        }

        //switch...case..
        var key = 1
        when(key){
            0->{

            }
            1->{

            }
            else->{

            }

        }

         var a = 1
        //类似三目运算符,kotlin没有三目
        var b = if (a > 0) 1 else 2

        //类型转换 
        //toByte(): Byte
        //toShort(): Short
        //toInt(): Int
        //toLong(): Long
        //toFloat(): Float
        //toDouble(): Double
        //toChar(): Char
        val b: Byte = 1 // OK, 字面值是静态检测的
        val i: Int = b.toInt() // OK

4、接口的定义

/**
 * 接口定义
 */
interface I {
    fun a(string: String?)
}
        var a = A()
        a.setInterface(object : I{
            override fun a(string: String?) {

            }
        })

5、继承和实现

/**
 * 实现接口
 */
internal class Child : I {

    override fun a(string: String?) {

    }
}

/**
 * 类的继承,接口实现
 */
class B : A(),I{
    override fun a(string: String?) {

    }
}

class C : View {
    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}
}

6、工具类定义(类似静态方法)

/**
 * 工具类,类似静态方法
 */
object Utils {
    fun eat() {

    }
}

         //静态方法调用
        Utils.eat()

7、类的操作

//类的声明及属性的操作
        var xiaoming  = A()
        xiaoming.age = 12
        xiaoming.name = "xiaoming"
        xiaoming.gender = 0

        var age = xiaoming.age
        var name = xiaoming.name

8、扩展函数

扩展函数可以在已有类中添加新的方法,不会对原类做修改,定义一个*.kt文件,如:在MainActivity中添加一个log()方法,内容如下:

fun  MainActivity.log() {

}

这个很好用,例如log之类的,或者某些类专用且子类通用的方法。上面的定义只有在MainActivity及子类中可以调用到log方法

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //inline文件中声明的扩展方法
        log()
    }
}

9、view的操作

MainActivity的布局中有一个TextView id=tv

<TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

activity中不用findViewById,也不用声明view的名称,只需要导入这个布局,就可以直接拿到id对象

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //setText
        tv.text = "abcd"
    }
}

监听方法,以下两种写法是一样的

        tv.setOnClickListener(object : View.OnClickListener{
            override fun onClick(view: View?) {
                view.setBackgroundColor(Color.BLACK)
            }
        })


        tv.setOnClickListener {
            //上面的view在这里是 it
            it.setBackgroundColor(Color.BLACK)
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值