当前位置:网站首页>kotlin 继承、类、重载
kotlin 继承、类、重载
2022-06-24 12:50:00 【华为云】
@[TOC](kotlin 继承与重载)
前言
使用纯代码 加 注释的方式,可以更快的理解源码
如果你喜欢,请点个赞,后期会不断的深入讲解
1、open 关键字
val study = Study("tiger") study.myPrintln()// kotlin 中所有的类,默认是 final 修饰的,是不能被继承的,和Java 相反// open 移除final 修饰符open class Person(private val name: String){ private fun showName() = "父类 的姓名是:$name"// 父类需要被子类继承的方法 open fun myPrintln() = println(showName())}class Study(private val subName: String) : Person(subName){ private fun showName() = "子类 的姓名是:$subName" override fun myPrintln() { super.myPrintln() println("我是继承自父类的方法: $subName") }}
2、类型转换
val study = Study("tiger")// 普通方法// study.myPrintln()// is + as 转换// is + as = 一般是配合在一起使用的 println(study is Person) println(study is Study) if (study is Person){ // 在这里,子类也重写了父类,会输出一次子类的调用 (study as Person).myPrintln() } if (study is Study){ (study as Study).myPrintln() }// kotlin 中所有的类,默认是 final 修饰的,是不能被继承的,和Java 相反// open 移除final 修饰符open class Person(private val name: String){ private fun showName() = "父类 的姓名是:$name"// 父类需要被子类继承的方法 open fun myPrintln() = println(showName())}class Study(private val subName: String) : Person(subName){ private fun showName() = "子类 的姓名是:$subName" override fun myPrintln() { super.myPrintln() println("我是继承自父类的方法: ${showName()}") }}
3、Any 超类
// 在kotlin 中,所有的类都隐式继承了 :Any() 你不写,默认就有// Any 类在kotlin 设计中,只提供标准,你看不到实现,实现在各个平台实现就好了class Person : Any()
4、对象声明
// object 即是单例的实例, 也是类名。 只有一个会被创建,这就是经典的单例 println(Person) println(Person) println(Person) Person.show()object Person { init { println("我是init 。。。。") } fun show() = println("我是show 方法")}
5、对象表达式
// 匿名对象, object : 表达式 val p = object : Person(){ override fun add(name: String) { super.add(name) } override fun del(name: String) { super.del(name) } } p.add("tiger") p.del("张三")// 具名的实现方式 val p2 = Person() p2.add("王五") p2.del("刘美丽")open class Person(){ open fun add(name: String) = println("新增: $name") open fun del(name: String) = println("删除: $name")}
6、伴生对象
println(Study.name) Study.showInfo()class Study{ // 伴生对象 companion object { val name = "张三" fun showInfo() = println("学生:$name") }}
7、嵌套类和内部类
// 内部类的访问 Person(18).Study("tiger").getName()// 嵌套类的调用 Body().showInfo()// 内部类的特点:外部类,能访问内部类,内部类能访问外部类class Person(val age: Int){// 默认情况下,内部类不能访问外部类,要增加修饰符 inner 才能够访问外部内 inner class Study(val name: String){ fun getName() = println("这个学生叫:$name 年龄:$age") }}// 嵌套类: kotlin 默认情况下,就是一个嵌套类// 外部的类,能访问嵌套的内部类,嵌套的内部类不能访问外部的类class Body{ val info = "tiger" fun showInfo() = Heart(info).showIfo() class Heart(val info: String){ fun showIfo() = println("我就是个测试 $info") }}
8、数据类
// 普通类// 普通类只会生成 set get 构造函数class ResponseResultBean(val name: String, val age: Int)// 数据类// 数据类 会生成 set get 构造函数 copy toString hashCode equalsdata class ResponseResultBean1(val name: String, val age: Int)
9、copy 函数
// 这里只周主构造 val p1 = Person("tiger") // 输出结果 次构造返回了 name: tiger, age: 8, coreInfo: 很重要// 这里走主构造和次构造// copy toString hashCode equals 等等, 主构造是不管次构造的,// 使用copy的时候,由于内部代码只处理主构造,必须考虑次构造的内容 val p2 = p1.copy("张三", 12) println(p2) // 输出结果 Person(name=张三, age=12)// data 数据类 主构造函数data class Person(val name: String, val age: Int){ var coreInfo = "" init { println("主构造函数走了: $name") } // 次构造函数 constructor(name: String) : this(name, 8) { coreInfo = "很重要" println("次构造返回了 name: $name, age: $age, coreInfo: $coreInfo") }}
10、运算符重载
println(AddClass(3, 3) + AddClass(5, 4)) class AddClass(val number1: Int, val number2: Int){// operator fun plus 运算符重载的方式,有多重方式,自行查看 operator fun plus(addClass: AddClass): Int { return (addClass.number1 + number1) + (addClass.number2 + number2) } }
11、枚举类定义函数
// 显示调用枚举值 Limbs.LIFT_HAND.show() Limbs.RIGHT_HAND.show() Limbs.LIFT_FOOT.show() Limbs.RIGHT_FOOT.show()class LimbsInfo(val limbsInfo: String, val length: Int)enum class Limbs(private val limbsInfo: LimbsInfo){ LIFT_HAND(LimbsInfo("左手", 100)), RIGHT_HAND(LimbsInfo("右手", 100)), LIFT_FOOT(LimbsInfo("左脚", 100)), RIGHT_FOOT(LimbsInfo("右脚", 100)); fun show() = println("身体情况:${limbsInfo.limbsInfo} 的长度是:${limbsInfo.length}")}
12、代数数据类型
println(Teacher(Exam.FRACTION1).show()) println(Teacher(Exam.FRACTION4).show())enum class Exam{ FRACTION1, FRACTION2, FRACTION3, FRACTION4,}class Teacher(private val exam: Exam){ fun show() = when(exam){ Exam.FRACTION1 -> "不及格的学生" Exam.FRACTION2 -> "及格的学生" Exam.FRACTION3 -> "良好的学生" Exam.FRACTION4 -> "优秀学生"// -> else 由于我的show 函数,是使用枚举类 类型来做判断处理的,这个就属于 代数类型,就不需要写else 了// 因为 when 表达式非常明确了,在这里只有四种类型,不会出现 else 其他的类型,所以就不需要了 }}
13、密封类
println(Teacher(Exam.Fraction1).show()) println(Teacher(Exam.Fraction2).show()) println(Teacher(Exam.Fraction3).show()) println(Teacher(Exam.Fraction4("tiger")).show())// sealed 密封类,里面的成员,就必须有 类型,并且继承自 本类sealed class Exam { // object ? 只会初始化一次,在不需要任何成员的情况下,一般都写成 object object Fraction1 : Exam() object Fraction2 : Exam() object Fraction3 : Exam() class Fraction4( val studyName: String) : Exam()}class Teacher(private val exam: Exam) { fun show() = when (exam) { is Exam.Fraction1 -> "不及格的学生" is Exam.Fraction2 -> "及格的学生" is Exam.Fraction3 -> "良好的学生" is Exam.Fraction4 -> "优秀学生,该学生的姓名是:${this.exam.studyName}" }}
14、数据类的小结
1、服务器请求回来的数据 JavaBean LoginResponseBean 基本上可以使用 数据类2、数据类 是至少必须有一个参数的主构造函数3、数据类必须有参数 val var 这样类型 的参数4、数据类不能使用 abstract open sealed inner 等等 修饰, (数据类,干的就是数据载入,数据存储的事情)5、需求 比较 copy toString 结构, 等等 这些丰富的功能时,也可以使用数据类
总结
🤩
️
边栏推荐
- The agile way? Is agile development really out of date?
- Gateway processing flow of zuul source code analysis
- Vipshop's "special sale" business is no longer easy to do?
- 青藤入选工信部网安中心“2021年数字技术融合创新应用典型解决方案”
- Getting started with the go Cobra command line tool
- Implement Domain Driven Design - use ABP framework - create entities
- go Cobra命令行工具入门
- 不用Home Assistant,智汀也开源接入HomeKit、绿米设备?
- Implement Domain Driven Design - use ABP framework - create entities
- Express 100 Express query interface (API) interface specification document - detailed version
猜你喜欢
Interviewer: the MySQL database is slow to query. What are the possible reasons besides the index problem?
Party, Google's autoregressive Wensheng graph model
Golden age ticket: Web3.0 Security Manual
The data value reported by DTU cannot be filled into Tencent cloud database through Tencent cloud rule engine
CVPR 2022 | 美团技术团队精选论文解读
Yolov6: the fast and accurate target detection framework is open source
几种常见的DoS攻击
Use terminal to activate CONDA service in pypharm (the ultimate method is definitely OK)
Sinomeni vine was selected as the "typical solution for digital technology integration and innovative application in 2021" of the network security center of the Ministry of industry and information te
Nifi from introduction to practice (nanny level tutorial) - environment
随机推荐
Open source monitoring system Prometheus
我开导一个朋友的一些话以及我个人对《六祖坛经》的一点感悟
Developer survey: rust/postgresql is the most popular, and PHP salary is low
用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧
go Cobra命令行工具入门
实现领域驱动设计 - 使用ABP框架 - 更新操作实体
Who is the fish and who is the bait? Summary of honeypot recognition methods from the perspective of red team
手把手教你用AirtestIDE无线连接手机!
CPU process priority
开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
8 lines of code to teach you how to build an intelligent robot platform
Appium installation
What should I do if I fail to apply for the mime database? The experience from failure to success is shared with you ~
Vipshop's "special sale" business is no longer easy to do?
数据科学家面临的七大挑战及解决方法
Richard Sutton, the father of reinforcement learning, paper: pursuing a general model for intelligent decision makers
Definition and use of constants in C language
3. caller service call - dapr
Comparator 排序函数式接口
LVGL库入门教程 - 颜色和图像