Kotlin笔记1

变量和函数的声明

Kotlin声明变量只有两个关键字:val(value,声明不可变的变量)var(variable,声明可变的变量),Kotlin的类型推导机制可以自动推导变量的类型,也可以显式声明变量类型。Java中声明变量使用string、int、double等,是否可变加final。

fun main() {
    // 声明不可变的变量
    val value1 = "abc"
    val value2 = 'j'
    val value3 = 4
    val value4:Int = 99  //显式声明变量类型
    // 声明可变的变量
    var value5 = 100
    value5 = 60
    var value6:String = "12gk"  //显式声明变量类型
}

Kotlin没有基本数据类型,全部使用对象数据类型,Int、String等首字母全部大写。

Kotlin定义函数的关键字是 fun(function的简写),fun后面是函数名。下面是一个相同功能函数的两种写法。

fun funName(num1:Int , num2:Int):Int {
    // funName 函数名,
    // num1:Int 参数1:参数1类型,
    // num2:Int 参数2:参数2类型,
    // Int 返回值类型
    return num1+num2
}
fun funName2(num1:Int , num2:Int):Int = num1+num2

无返回值时:

fun main() {
    funName("Kotlin", 666)
}
fun funName(value1:String, value2:Int){
    println("hello $value1 $value2")
}

逻辑执行语句

if-else语句:

fun main() {
    val result:Int = funName(555, 666)
    println(result)
}
fun funName(value1:Int, value2:Int):Int{
    return if (value1>value2){
        value1
    } else{
        value2
    }
}

精简的写法:

fun funName(value1:Int, value2:Int):Int = if(value1>value2) value1 else value2

if-else if-else语句:

fun funName(value1:Int, value2:Int):Int = if(value1>value2) value1 
else if (value1==value2) 0
else value2

when语句:

// 返回较大的值
fun funName(value1:Int, value2:Int):Int = when(value1>value2){
    false -> value2
    true -> value1
}
// when中不传参数写成
fun funName(value1:Int, value2:Int):Int = when{
    value1>value2 -> value1
    value1<value2 -> value2
    else -> 0
}
// is 相当于Java中的instanceof关键字
fun checkNumber(num: Number) {
    when (num) {
        is Int -> println("number is Int")
        is Double -> println("number is Double")
        else -> println("number not support")
    }
}

for循环语句:

// 双端闭区间
for (i in 0..10) {
    println(i)  //0-10
}
// 左闭右开
for (i in 0 until 10) {
    println(i)  //0-9
}
// 0 2 4 6 8 10
for (i in 0 ..10 step 2) {
    println(i)
}
// 逆序间隔为2,双端闭区间, 10 8 6 4 2 0
for (i in 10 downTo 0 step 2) {
    println(i)
} 

while循环:

var i = 1
while (i<=5){
    println(i)
    i++
}

类与对象

Kotlin使用class关键字声明对象。

class Person {
    var name = "Tom"
    var age = 3
    fun eat() {
        println("$name is eating. He is $age years old.")
    }
}

对象的实例化,面向对象编程:

val person = Person()
person.eat()
person.age = 5
person.eat()
// Tom is eating. He is 3 years old.
// Tom is eating. He is 5 years old.

继承与构造函数

open关键字代表类可被继承

open class Person {
    var name = "Tom"
    var age = 3
    fun eat() {
        println("$name is eating. He is $age years old.")
    }
}

一个类继承另一个类

class Student : Person() {
    var score = 100
    fun run(){
        println("$name is running, score is $score")
    }
}

构造函数

如果没有主构造函数,继承Person类的时候就不需要加上括号

可被继承的Person类:

open class Person(name0:String , age0:Int) {
    var name = name0
    var age = age0
    fun eat() {
        println("$name is eating. He is $age years old.")
    }
}

继承Person类的Student类:

class Student(score0:Int, name:String , age:Int) : Person(name , age) {
    private var score = score0
    fun run(){
        println("$name is running, score is $score, age is $age")
    }
    constructor(name:String , age:Int) : this(){
        this.name = name
        this.age = age
    }
    constructor(name:String) : this(){
        this.name = name
    }
    constructor(age:Int) : this(){
        this.age = age
    }
    constructor() : this(100,"无参构造器",0)
}

Student类的实例化:

val stu0 = Student()
val stu1 = Student("Jerry")
val stu2 = Student(66)
val stu3 = Student("Harry",5)
val stu4 = Student(60,"Jack",6)
stu0.run()
stu1.run()
stu2.run()
stu3.run()
stu4.run()

运行结果:

// 无参构造器 is running, score is -100, age is -1
// Jerry is running, score is -100, age is -1
// 无参构造器 is running, score is -100, age is 66
// Harry is running, score is -100, age is 5
// Jack is running, score is 60, age is 6

接口

Kotlin接口与Java接口几乎完全一致。Kotlin中继承父类、实现接口统一使用冒号,接口的后面不用加上括号,接口没有构造函数可以调用。Kotlin中使用override关键字重写父类中的函数或实现接口中的函数

interface Study {
    fun read()
}
class Student: Study {
    override fun read() {
        println("学生会读书")
    }
}

Java 中有public、private、protected和default(不写)这4种函数修饰符。

Kotlin 中也有4种,分别是public、private、protected和 internal。

修饰符

JavaKotlin
public所有类可见所有类可见(默认)
private当前类可见当前类可见
protected当前类、子类、同一包路径下的类可见当前类、子类可见
default同一包路径下的类可见(默认)
internal同一模块中的类可见

数据类

Kotlin使用data关键字声明数据类,只需一行代码。Java需要重写equals、toString方法。

data class Cellphone(val brand: String, val price: Int)
val cellphone1 = Cellphone("Xiaomi", 1999)
val cellphone2 = Cellphone("Xiaomi", 1999)
println(cellphone1)
println("cellphone1 equals cellphone2 " + (cellphone1 == cellphone2))
// 输出结果
// Cellphone(brand=Xiaomi, price=1999)
// cellphone1 equals cellphone2 true

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值