当前位置:网站首页>七、Kotlin基础学习:1、创建类;2、构造函数;3、继承;4、封装;5、抽象类;6、接口;7、嵌套类;8、内部类;9、枚举类
七、Kotlin基础学习:1、创建类;2、构造函数;3、继承;4、封装;5、抽象类;6、接口;7、嵌套类;8、内部类;9、枚举类
2022-07-30 05:43:00 【¥伊人独醉】
1、创建类;
字段、属性、方法、构造方法
首先来个【案例】:

布局文件 activity_main.xml

学生类 Student.kt

MainActivity.kt

效果:

2、构造函数;
(1)主构造函数
(2)次构造函数
学生类 Student.kt

activity_main.xml

MainActivity.kt
日志:

3、继承;
kotlin中对于定义的类默认使用final进行修饰,也就是说所有的类不允许继承,如果需要继承,需要将父类添加open关键字。子类使用“:”表示继承
重写父类方法,也需要父类方法添加open关键字。
【案例】

Student.kt

ColleageStudent.kt

activity_main.xml

MainActivity.kt
日志:

4、封装;
对字段进行封装
【案例】
父类:Book.kt

子类:Cartoon.kt
activity_main.xml

MainActivity.kt

效果:

5、抽象类

抽象类是没有实现的类,不能实例化,为子类提供共同的约束。
【案例】

MainActivity.kt

效果:

6、接口
关键字interface。
也是对实现类一种约定,不能实例化。接口的子类可以多重继承。
【案例】

IExchange.kt

IUsb.kt
Phone.kt

Drive.kt
MainActivity.kt
效果:

7、嵌套类
写在类的内部,但是该类不能访问外部类的成员。

8、内部类
写在类的内部,该类可以访问外部类的成员。需要添加关键字inner
【案例】
MainActivity.kt

日志:
9、枚举类
关键字enum,划定一个取值范围。某某变量只能取几个值中一个
【案例】
Color.kt

MainActivity.kt

日志:
边栏推荐
- Remember a traffic analysis practice - Anheng Technology (August ctf)
- Jackson serialization failure problem - oracle data return type can't find the corresponding Serializer
- 21. Kotlin Advanced Learning: Implementing Simple Network Access Encapsulation
- 国内数字藏品交易平台开发市场会开放二级市场吗
- Arthas 命令解析(jvm/thread/stack/heapdump)
- Mycat2.0搭建教程
- Redis publish/subscribe
- Defense Ideas for a Type of SMS Vulnerability
- Redis 客户端常见异常分析
- Flink PostgreSQL CDC配置和常见问题
猜你喜欢

Mycat2.0搭建教程

The first WebAssembly program

nodejs PM2监控及报警邮件发送(二)
![Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc](/img/2d/50c9001125cd613087044d2b6c78b1.png)
Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc

标准输入输出流(System.in,System.out)

【数仓】数据仓库高频面试题题英文版(1)

十七、Kotlin进阶学习:1、守护线程;2、线程和协程之间的效率对比;3、取消协程;

uni-app: The use of uni-icons and how to customize icons

protobuf编码及网络通信应用(一)

Offensive and defensive world easy_web
随机推荐
【SQL】SQL 高频面试题英语版(1)
Jdbc & Mysql timeout分析
C#预定义数据类型简介
The Request request body is repackaged to solve the problem that the request body can only be obtained once
sqli-labs less3/4 Targeting Notes
A Spark task tuning 】 【 one day suddenly slow down how to solve
[Getting C language from zero basis - navigation summary]
二十一、Kotlin进阶学习:实现简单的网络访问封装
Reasons and solutions for Invalid bound statement (not found)
Trust anchor for certification path not found. Exception solution.
[Ten years of network security engineers finishing] - 100 penetration testing tools introduction
【零基础搞定C语言——导航汇总篇】
十八、Kotlin进阶学习:1、挂起函数执行的顺序;2、使用 async 和 await 异步执行挂起函数;3、协程的调度器;4、父子协程;
sql中 exists的用法
The types of data structures and MySQL index
Online sql editing query tool sql-editor
十九、Kotlin进阶学习:1、管道数据的收和发;2、管道的关闭;3、生产者和消费者;4、管道的缓存区;
Arrays工具类的使用
MySQL 特殊语句及优化器
批量自动归集