当前位置:网站首页>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
边栏推荐
- scipy. misc. imread()
- 什么是防火墙?防火墙基础知识讲解
- Understanding rotation matrix R from the perspective of base transformation
- Applet global style configuration window
- 【ManageEngine】如何利用好OpManager的报表功能
- OpenGL - Model Loading
- Applet (use of NPM package)
- Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
- Ecmascript6 introduction and environment construction
- 2309. The best English letters with both upper and lower case
猜你喜欢
AUTOSAR from getting started to mastering 100 lectures (103) -dbc file format and creation details
Editor use of VI and VIM
Svg optimization by svgo
混淆矩阵(Confusion Matrix)
Applet customization component
Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)
Priority queue (heap)
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
fs. Path module
Ros-11 common visualization tools
随机推荐
STM32简易多级菜单(数组查表法)
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
云计算技术热点
TF coordinate transformation of common components of ros-9 ROS
Information and entropy, all you want to know is here
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
notepad++
Blogger article navigation (classified, real-time update, permanent top)
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
Applet customization component
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
C # compare the differences between the two images
scipy.misc.imread()
OpenFeign
Solution to the problem of the 10th Programming Competition (synchronized competition) of Harbin University of technology "Colin Minglun Cup"
Kubedm series-00-overview
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
Applet (global data sharing)
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
Programming implementation of ROS learning 5-client node