0
点赞
收藏
分享

微信扫一扫

Kotlin学习:3.6.类的分类

上善若水的道 2022-02-09 阅读 69

类的分类

类修饰符(classModifier)

类修饰符作用
final不能被继承 final类
open可以被继承 open类
abstract抽象类
enum枚举类
data数据类
sealed密封类
annotation注解类

抽象类

使用abstract关键字将类标记为抽象类

abstract class 抽象类名[(主构造函数)][:继承父类和实现接口]{
	abstract fun 方法名
	abstract var/val 属性名:属性类型
  	...
}


abstract class AbBaseFruit {

    constructor(name: String) {
        this.name = name;
    }

    var name: String = ""

    abstract val TIME: Int

    abstract var size: Int

    abstract fun eat()

//    abstract fun wash(){
//    // 错误 方法不能被修饰
//    }

    fun wash() {

    }

    fun print(){
        println("name = $name")
        println("TIME = $TIME")
        println("size = $size")
    }

}

class AbApple(name: String) : AbBaseFruit(name) {

    override val TIME: Int
        get() = 123

    override var size: Int
        get() = 10
        set(value) {
            size = value
        }

    override fun eat() {
    }

}

调用

        val apple2 = AbApple("测试抽象类")
        apple2.print()

输出

2022-02-09 11:06:05.591 24363-24363/com.example.kotlintestdemo I/System.out: name = 测试抽象类
2022-02-09 11:06:05.592 24363-24363/com.example.kotlintestdemo I/System.out: TIME = 123
2022-02-09 11:06:05.592 24363-24363/com.example.kotlintestdemo I/System.out: size = 10

枚举类

enum class 类名{
    枚举常量列表
}

enum class ColorS {
    RED, BLACK, BLUE, GREEN, WHITE
}

数据类

1.自动实现

只要是数据类,将会自动实现如下方法:

equals() / hashCode()
toString() 格式如 "User(name=John, age=42)"
componentN() functions 对应于属性,按声明顺序排列
copy() 函数

2.约束

数据类的要求(约束条件)

主构造函数至少包含一个参数
所有的主构造函数的参数必须标识为val或var
数据类不可以声明为抽象abstract、开放open、密封sealed、内部inner
数据类不能继承其他类,但可实现接口。

3.栗

data class DataExample(var name: String ,var age :Int) {

}

调用

        val dataExample = DataExample("小明",12)
        println("dataExample === $dataExample")
        dataExample.name = "小栗"
        println("dataExample === $dataExample")
        val dataExample2 = dataExample.copy(name = "小王")
        println("dataExample2 === $dataExample2")

输出、

2022-02-09 14:28:58.776 29288-29288/com.example.kotlintestdemo I/System.out: dataExample === DataExample(name=小明, age=12)
2022-02-09 14:28:58.776 29288-29288/com.example.kotlintestdemo I/System.out: dataExample === DataExample(name=小栗, age=12)
2022-02-09 14:28:58.776 29288-29288/com.example.kotlintestdemo I/System.out: dataExample2 === DataExample(name=小王, age=12)

4.解构声明

在 Koltin 中可以把一个对象赋值给多个变量,这种操作叫做解构声明(Destructuring declaration)

        val (name,age)=dataExample  //解构声明
        println("解构声明 == $name,$age")

输出

2022-02-09 14:35:58.331 30036-30036/com.example.kotlintestdemo I/System.out: 解构声明 == 小栗,12

密封类

密封类用来表示受限的类继承结构:当一个值为有限几种的类型, 而不能有任何其他类型时。在某种意义上,他们是枚举类的扩展:枚举类型的值集合 也是受限的,但每个枚举常量只存在一个实例,而密封类 的一个子类可以有可包含状态的多个实例。

声明一个密封类,使用 sealed 修饰类,密封类可以有子类,但是所有的子类都必须要内嵌在密封类中。

sealed 不能修饰 interface ,abstract class(会报 warning,但是不会出现编译错误)

使用密封类的关键好处在于使用 when 表达式 的时候,如果能够 验证语句覆盖了所有情况,就不需要为该语句再添加一个 else 子句了。

Kotlin 数据类与密封类:
https://www.runoob.com/kotlin/kotlin-data-sealed-classes.html

Kotlin Vocabulary | 密封类 sealed class
https://zhuanlan.zhihu.com/p/122244787

注解类

嵌套类

只要将一个类放在另一个类中定义,这个类就变成了嵌套类,相当于Java中static修饰的静态内部类。

它没有外部的引用

class KtExample(name: String) {
    class NestingClass {
        fun log(){
            
        }
    }
}

调用

KtExample.NestingClass ().log()

内部类

使用inner修饰的嵌套类叫内部类,相当于Java中无static修饰的非静态内部类。

内部类相当于外部类的实例成员,所以它可以直接访问外部类的所有成员。

class KtExample(name: String) {
    inner class NestingClass {
        fun log(){

        }
    }
}

调用

KtExample().NestingClass ().log()

Kotlin的嵌套类和内部类
https://blog.csdn.net/weixin_40763897/article/details/107825116

举报

相关推荐

0 条评论