0
点赞
收藏
分享

微信扫一扫

【Kotlin】Kotlin笔记4-面向对象编程2(接口,函数的可见性修饰符)


Kotlin笔记4-面向对象编程2-接口,数据类与单例类


3.2 面向对象编程2

  • 接口

interface

​Example:​

interface Study {
fun readBooks()
fun doHomework(){
println("do homework default implementation.")
}
}

​继承父类与实现接口​

class Student5(name: String, age: Int) : Person2(name, age), Study {
override fun readBooks() {
println("$name is reading.")
}

override fun doHomework() {
println("$name is doing homework.")
}
}

​面向接口编程: 多态​

fun main() {
val student = Student5("Jack", 19)
doStudy(student)
}

fun doStudy(study: Study) {
study.readBooks()
study.doHomework()
}

​接口默认实现​

interface Study {
fun readBooks()
fun doHomework() {
println("do homework default implementation."
}
}
  • 函数的可见性修饰符

Java

public

public

public

private

private

protected

protected

default

internal

Kotlin,Java修饰符对照

修饰符

Kotlin

Java

private

对当前类内部可见

当前类可见

public

默认

当前类可见

portected

对当前类和子类可见

…同一包路径下的类可见

internal

对同一模块的类可见


default


默认,同一包路径下的类可见


举报

相关推荐

面向对象——权限修饰符

JavaSE面向对象_访问权限修饰符

0 条评论