当前位置:网站首页>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
边栏推荐
- Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
- Applet customization component
- Applet (global data sharing)
- [code practice] [stereo matching series] Classic ad census: (5) scan line optimization
- Transfer learning and domain adaptation
- Configuration and startup of kubedm series-02-kubelet
- Blogger article navigation (classified, real-time update, permanent top)
- C # draw Bezier curve with control points for lattice images and vector graphics
- What is a firewall? Explanation of basic knowledge of firewall
- 信息与熵,你想知道的都在这里了
猜你喜欢

Hosting environment API

22-07-04 西安 尚好房-项目经验总结(01)

ROS learning 4 custom message
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]

Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning

Applet (use of NPM package)

Confusion matrix

TF coordinate transformation of common components of ros-9 ROS

Use and programming method of ros-8 parameters

Applet network data request
随机推荐
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
Pearson correlation coefficient
OpenGL - Lighting
Ros-11 common visualization tools
Codeworks round 638 (Div. 2) cute new problem solution
图神经网络+对比学习,下一步去哪?
scipy. misc. imread()
交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
Applet (use of NPM package)
迁移学习和域自适应
Explain NN in pytorch in simple terms CrossEntropyLoss
Information and entropy, all you want to know is here
Progressive JPEG pictures and related
Wxss template syntax
Hosting environment API
How many checks does kubedm series-01-preflight have
Multiple linear regression (sklearn method)
nodejs_ 01_ fs. readFile
Attention is all you need
Ecmascript6 introduction and environment construction