不会真的学不会kotlin吧?(二)类、接口和类型

本文是根据慕课网kotlin相关课程本人笔记和个人理解所作
代码部分参考慕课网新kotlin入门课程

文章目录

    • 本章简介
    • 结构导图
    • 类抽象类和接口
        • 抽象类
        • 接口
        • 拓展方法
        • 类成员
    • 空类型
        • 空类型
    • 类型转换
        • 类型转换

本章简介

这一章主要是kotlin的类的一些操作,转换、拓展之类的,令我印象深刻的还是智能转换,感觉挺不错的

结构导图

不会真的学不会kotlin吧?(二)类、接口和类型_第1张图片

类抽象类和接口

java

public class SimpleClass
        extends AbsClass
        implements SimpleInf {
    public int x;

    public SimpleClass(int x) {
        this.x = x;
    }

    public void y(){

    }

    @Override
    public void simpleMethod() {

    }

    @Override
    public void absMethod() {

    }
}

kotlin

open class SimpleClass(var x: Int, val y: String)
    : AbsClass(), SimpleInf {
    override val simpleProperty: Int
        get() {
            return 2
        }

    val z : Long
        get() {
            return simpleProperty * 2L
        }

    override fun absMethod() {}
    override fun simpleMethod() {}
    fun y(){}

    fun zzz(string: String){

    }

    final override fun overridable(){

    }
}

抽象类

java

public abstract class AbsClass {
    public abstract void absMethod();
    protected void overridable(){ }
    public final void nonOverridable(){ }
}

kotlin

abstract class AbsClass {
    abstract fun absMethod()
    open fun overridable(){}
    fun nonOverridable(){}
}

接口

java

public interface SimpleInf {
    void simpleMethod();
}

kotlin

interface SimpleInf {
    val simpleProperty: Int // property

    fun simpleMethod()
}

拓展方法

kotlin

class PoorGuy {
    var pocket: Double = 0.0
}

fun PoorGuy.noMoney() {

}

//property = backing field + getter + setter
var PoorGuy.moneyLeft: Double
    get() {
        return this.pocket
    }
    set(value) {
        pocket = value
    }

interface Guy {
    var moneyLeft: Double
        get() {
            return 0.0
        }
        set(value) {

        }

    fun noMoney() {
        println("no money called.")
    }
}


fun String.padding(count: Int, char: Char = ' '): String {
    val padding = (1..count).joinToString("") { char.toString() }
    return "${padding}${this}${padding}"
}

fun String.isEmail(): Boolean {
    return matches(Regex("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"))
}

fun String.times(count: Int): String {
    return (1..count).joinToString("") { this }
}

fun main() {

    "[email protected]".isEmail()


    println("Hello".padding(1))
    println("*".times(10))

    val stringTimes = String::times
    val stringTimesBound = "*"::times

    arrayOf(1, 3, 3).forEach { }

    val x = 2
}

类成员

java

public class SimpleClass
        extends AbsClass
        implements SimpleInf {
    public int x;

    public SimpleClass(int x) {
        this.x = x;
    }

    public void y(){

    }

    @Override
    public void simpleMethod() {

    }

    @Override
    public void absMethod() {

    }
}

kotlin

open class SimpleClass(var x: Int, val y: String)
    : AbsClass(), SimpleInf {
    override val simpleProperty: Int
        get() {
            return 2
        }

    val z : Long
        get() {
            return simpleProperty * 2L
        }

    override fun absMethod() {}
    override fun simpleMethod() {}
    fun y(){}

    fun zzz(string: String){

    }

    final override fun overridable(){

    }
}

空类型

空类型

kotlin

fun main() {
//    var nonNull: String = "Hello"
//    // nonNull = null
//    val length = nonNull.length

    var nullable: String? = "Hello"
    //val length = nullable?.length
    val length = nullable?.length ?: 0 //elvis  boolean? a : b


    var x: String = "Hello"
    var y: String? = "World"

//    x = y // Type mismatch
    y = x // OK

    var a: Int = 2
    var b: Number = 10.0

//    a = b // Type mismatch
    b = a // OK


    val person = Person()
    val title = person.title

    val titleLength = title?.length

    val file = File("abc")

    val files = file.listFiles()
    println(files.size)


}

类型转换

类型转换

java

        Kotliner kotliner = new Person("benny", 20);
        if (kotliner instanceof Person) {
            System.out.println(((Person) kotliner).name);
        }

kotlin

    val kotliner: Kotliner = Person("benny", 20)
    if (kotliner is Person) {
        println((kotliner as? Person)?.name)
    }

    var value: String? = null
    value = "benny"
    if (value != null) {
        println(value.length)
    }

你可能感兴趣的:(不会真的学不会kotlin把?)