当前位置:网站首页>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 结构, 等等 这些丰富的功能时,也可以使用数据类总结
🤩
️
边栏推荐
- Main steps of system test
- 1. Snake game design
- 手把手教你用AirtestIDE无线连接手机!
- Boss direct employment IPO: both the end and the beginning
- Getting started with the go Cobra command line tool
- SYSTEMd common component description
- Yolov6: the fast and accurate target detection framework is open source
- Open source monitoring system Prometheus
- I have fundamentally solved the problem of wechat occupying mobile memory
- J'a i ouvert quelques mots d'un ami et quelques réflexions personnelles sur le livre des six ancêtres
猜你喜欢

Understanding openstack network

不用Home Assistant,智汀也开源接入HomeKit、绿米设备?

Without home assistant, zhiting can also open source access homekit and green rice devices?

使用 Abp.Zero 搭建第三方登录模块(一):原理篇

C语言中常量的定义和使用

1、贪吃蛇游戏设计

Opengauss kernel: simple query execution

YOLOv6:又快又准的目标检测框架开源啦

Brief introduction to cluster analysis

LVGL库入门教程 - 颜色和图像
随机推荐
Cloud native essay solicitation progress case practice
我開導一個朋友的一些話以及我個人對《六祖壇經》的一點感悟
敏捷之道 | 敏捷开发真的过时了么?
Why is open source technology so popular in the development of audio and video streaming media platform?
Boss direct employment IPO: both the end and the beginning
数据科学家面临的七大挑战及解决方法
Coinbase将推出首个针对个人投资者的加密衍生产品
[one picture series] one picture to understand Tencent Qianfan ipaas
hands-on-data-analysis 第三单元 模型搭建和评估
Definition and use of constants in C language
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
1、贪吃蛇游戏设计
Comparator sort functional interface
3. caller service call - dapr
Configuration (enable_*) parameter related to execution plan in PG
初中级开发如何有效减少自身的工作量?
Detailed explanation of abstractqueuedsynchronizer, the cornerstone of thread synchronization
I enlighten a friend and my personal understanding of the six patriarchs' Tan Jing
Geological disaster early warning monitoring RTU
Teach you how to use airtestide to connect your mobile phone wirelessly!