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

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

本章简介

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

结构导图

在这里插入图片描述

类抽象类和接口

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() {

    "admin@bennyhuo.com".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)
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值