当前位置:网站首页>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
边栏推荐
- Understanding rotation matrix R from the perspective of base transformation
- Codeworks round 681 (Div. 2) supplement
- Blue Bridge Cup provincial match simulation question 9 (MST)
- 编辑器-vi、vim的使用
- 生成对抗网络
- C [essential skills] use of configurationmanager class (use of file app.config)
- Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
- [code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
- Ros-10 roslaunch summary
- 【阅读笔记】图对比学习 GNN+CL
猜你喜欢
![Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]](/img/68/6bfa390b0bedcdbc4afba2f9bd9c0f.jpg)
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
![Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]](/img/d8/39020b1ce174299f60b6f278ae0b91.jpg)
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]

Composition of applet code

OpenGL - Coordinate Systems

Ros-10 roslaunch summary

Huber Loss

NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA

OpenGL - Model Loading
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]
![Rebuild my 3D world [open source] [serialization-1]](/img/74/b6253845b43bc18f425d57695fba7c.jpg)
Rebuild my 3D world [open source] [serialization-1]
随机推荐
Oracle advanced (III) detailed explanation of data dictionary
Priority queue (heap)
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
Global configuration tabbar
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
12. Dynamic link library, DLL
Programming implementation of ROS learning 5-client node
OpenGL - Model Loading
牛顿迭代法(解非线性方程)
2309. The best English letters with both upper and lower case
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
Editor use of VI and VIM
My experience from technology to product manager
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
ROS learning 1- create workspaces and function packs
Codeworks round 681 (Div. 2) supplement
Applet (subcontracting)
阿里云发送短信验证码
Applet (use of NPM package)
Use and programming method of ros-8 parameters