当前位置:网站首页>七、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
日志:
边栏推荐
- mysql不是内部或外部命令,也不是可运行的程序或批处理文件解决
- 根据ip地址获取地理位置及坐标(离线方式)
- misc-log analysis of CTF
- C# WPF下限制TextBox只输入数字、小数点、删除等键
- 批量自动归集
- sql concat() function
- 第一个WebAssembly程序
- Nodejs PM2 monitoring and alarm email (2)
- [Ten years of network security engineers finishing] - 100 penetration testing tools introduction
- Trust anchor for certification path not found. Exception solution.
猜你喜欢
第一个WebAssembly程序
SSTI range
Jdbc & Mysql timeout analysis
Detailed introduction to the usage of Nacos configuration center
学生成绩管理系统(C语言版)
Go简单实现协程池
Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc
FastAPI 快速入门
MYSQL一站式学习,看完即学完
Volatility memory forensics - command shows
随机推荐
Trust anchor for certification path not found. Exception solution.
Flink CDC 实现Postgres到MySQL流式加工传输案例
The types of data structures and MySQL index
网上说的挖矿究竟是什么? 挖矿系统开发详解介绍
MySQL - Function and Constraint Commands
【面经】米哈游数据开发面经
Student achievement management system (C language version)
MySQL window function
mysql delete duplicate data in the table, (retain only one row)
SQL Server Installation Tutorial
【数仓】数据质量
Bypassing the file upload vulnerability
php vulnerability full solution
十九、Kotlin进阶学习:1、管道数据的收和发;2、管道的关闭;3、生产者和消费者;4、管道的缓存区;
MySQL - Multi-table query and case detailed explanation
Jackson serialization failure problem - oracle data return type can't find the corresponding Serializer
Reasons and solutions for Invalid bound statement (not found)
SQL Server 数据库之生成与执行 SQL 脚本
利用自定义注解,统计方法执行时间
Student management system