当前位置:网站首页>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
边栏推荐
- np.allclose
- asp. Net (c)
- Priority queue (heap)
- Alibaba cloud sends SMS verification code
- OpenFeign
- 【阅读笔记】图对比学习 GNN+CL
- What is a firewall? Explanation of basic knowledge of firewall
- Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
- 阿里云发送短信验证码
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
猜你喜欢
Confusion matrix
Applet customization component
Using request headers to develop multi terminal applications
Progressive JPEG pictures and related
Information and entropy, all you want to know is here
OpenGL - Lighting
Codeworks round 639 (Div. 2) cute new problem solution
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
Ros-10 roslaunch summary
Rebuild my 3D world [open source] [serialization-2]
随机推荐
RT thread kernel quick start, kernel implementation and application development learning with notes
It's too difficult to use. Long articles plus pictures and texts will only be written in short articles in the future
nodejs_ fs. writeFile
Applet (use of NPM package)
图神经网络+对比学习,下一步去哪?
Svg optimization by svgo
Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
Confusion matrix
Use arm neon operation to improve memory copy speed
2011-11-21 training record personal training (III)
Array, date, string object method
Summary of "reversal" problem in challenge Programming Competition
Programming implementation of ROS learning 5-client node
Codeworks round 681 (Div. 2) supplement
生成对抗网络
混淆矩阵(Confusion Matrix)
nodejs_ 01_ fs. readFile
C # compare the differences between the two images
Information and entropy, all you want to know is here