当前位置:网站首页>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
边栏推荐
- [code practice] [stereo matching series] Classic ad census: (5) scan line optimization
- 云计算技术热点
- nodejs_ 01_ fs. readFile
- C#图像差异对比:图像相减(指针法、高速)
- 一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
- Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
- .NET服务治理之限流中间件-FireflySoft.RateLimit
- 3D reconstruction open source code summary [keep updated]
- Nodejs modularization
- Applet customization component
猜你喜欢
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
Rebuild my 3D world [open source] [serialization-1]
Svgo v3.9.0+
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
AUTOSAR from getting started to mastering 100 lectures (103) -dbc file format and creation details
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
3D reconstruction open source code summary [keep updated]
Generate confrontation network
Applet customization component
随机推荐
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
Uni app implements global variables
RT thread kernel quick start, kernel implementation and application development learning with notes
Confusion matrix
Node collaboration and publishing
Codeworks round 681 (Div. 2) supplement
Causes and appropriate analysis of possible errors in seq2seq code of "hands on learning in depth"
Svg optimization by svgo
Hosting environment API
Rebuild my 3D world [open source] [serialization-2]
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
Composition of applet code
Array, date, string object method
Confusing basic concepts member variables local variables global variables
My experience from technology to product manager
混淆矩阵(Confusion Matrix)
Applet (use of NPM package)
File server migration scheme of a company
c#比较两张图像的差异