当前位置:网站首页>Kotlin introductory notes (VI) interface and function visibility modifiers
Kotlin introductory notes (VI) interface and function visibility modifiers
2022-07-05 09:14:00 【Meng Meng Mu Xi】
Preface : This tutorial is best done with JAVA Study on the basis of
One 、 Interface
Kotlin Interface and Java Almost exactly the same
Define an interface :
interface Study{
fun readBooks()
fun doHomework()
}
use Student Implement this interface :
class Student(name : String , age : Int) : Study {
override fun readBooks() {
println(name + " is reading.")
}
override fun doHomework(){
println(name + " is doing homework.")
}
}
be familiar with Java The person must specify ,Java For inheritance of extends , The interface is implemented with implements , and Kotlin Unified use : ( The colon ).
Now we can main() Function, write the following code to call the functions in these two interfaces
fun main() {
val student = Student("Jack",19)
doStudy(student)
}
fun doStudy(study : Study) {
study.readBooks()
study.doHomework()
}
however Kotlin Also added a function :
Allows default implementation of functions defined in the interface .
Don't talk much , Code up :
interface Study {
fun readBooks()
fun doHomework() {
println("do homework default implementation.")
}
}
At this time, you will find ,Student Will only force the implementation of readBooks A function .
Two 、 Visibility modifier of function
Default modifier :
Java : default
Kotlin : public
Kotlin Abandoned Java Medium default , Introduced a new concept :internal , Indicates that it is only visible to classes of the same module . protected stay Java And Kotlin It's different , See the following table for details :
Modifier | Java | Kotlin |
---|---|---|
public | All classes are visible | All classes are visible ( Default ) |
private | The current class is visible | The current class is visible |
protected | The current class 、 Subclass 、 Classes under the same package path are visible | The current class 、 Subclasses are visible |
default | Classes under the same package path are visible ( Default ) | nothing |
internal | nothing | Class visible in the first mock exam module |
If you like this series , You might as well pay attention ! Thank you for watching !
Reference resources :
《 First line of code Android ( The third edition )》 --- Guo Lin
边栏推荐
- Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
- 2309. 兼具大小写的最好英文字母
- Codeworks round 638 (Div. 2) cute new problem solution
- Introduction Guide to stereo vision (1): coordinate system and camera parameters
- asp. Net (c)
- OpenGL - Model Loading
- C # draw Bezier curve with control points for lattice images and vector graphics
- Confusion matrix
- My experience from technology to product manager
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
猜你喜欢
Creation and reference of applet
Rebuild my 3D world [open source] [serialization-2]
Priority queue (heap)
Attention is all you need
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
Huber Loss
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
Huber Loss
Hi Fun Summer, play SQL planner with starrocks!
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
随机推荐
Attention is all you need
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
C#绘制带控制点的Bezier曲线,用于点阵图像及矢量图形
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
Ros-11 common visualization tools
OpenGL - Coordinate Systems
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
ROS learning 1- create workspaces and function packs
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
Hi Fun Summer, play SQL planner with starrocks!
My life
Priority queue (heap)
Codeforces Round #648 (Div. 2) E.Maximum Subsequence Value
生成对抗网络
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
Global configuration tabbar
c语言指针深入理解
C#图像差异对比:图像相减(指针法、高速)
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)