kotlin 扩展类的功能

扩展功能 (Extension function)

  • Kotlin provides the ability to add more functionality to the existing class without inheriting them.

    Kotlin可以在不继承现有类的情况下为其添加更多功能。

  • This is done via a special declaration called "Extension'.

    这通过称为“扩展”的特殊声明来完成。

  • When a function added into an existing User-defined or Library class called 'Extension Function'.

    当函数添加到现有的用户定义或库类中时,称为“ 扩展函数 ”。

  • It can also define extensions of functions and properties for companion objects.

    它还可以定义伴随对象的功能和属性的扩展。

Kotlin扩展功能功能程序 (Program for extension function features in Kotlin)

package com.includehelp
// Declare class
class MyClass1{
// Member function
fun sayHello(){
println("Say Hello")
}
}
// Declare class
class MyClass2{
// create companion object to call method with class name
companion object{
// companion object function
fun display(){
println("Display from Companion Object !!")
}
}
}
// define Extension function for MyClass1
fun MyClass1.greetExtn(){
println("Greetings from Extension Function !!")
}
// define extension for Int Class
fun Int.isOdd(){
if(this%2==0){
println("Number is ODD")
}
}
// Define Extension function for MyClass2 Companion object
fun MyClass2.Companion.printData(){
println("Extension function for Companion object !!")
}
// Main Function, Entry point of Program
fun main(){
// Create Instance
val myClass1 = MyClass1()
// Called member function of class
myClass1.sayHello()
// Called extension function
myClass1.greetExtn()
// Called Int Class extension Function
24.isOdd()
// Called companion object member function
MyClass2.display()
// Called companion object extension function
MyClass2.printData()
}

Output:

输出:

Say Hello
Greetings from Extension Function !!
Number is ODD
Display from Companion Object !!
Extension function for Companion object !!

翻译自: https://www.includehelp.com/kotlin/extension-function-features.aspx

kotlin 扩展类的功能

kotlin 扩展类的功能_Kotlin程序| 扩展功能功能相关推荐

  1. 国产兼容三菱FX3U源码 ,修改监控时卡顿问题。 8位口令功能,程序消除功能

    国产兼容三菱FX3U源码,最新一波bug修改,修改监控时卡顿问题. 8位口令功能,程序消除功能,定时器特殊情况下不运行问题. 带modbus-tcp功能,实时时钟,深度测试后,修改一些主要bug后,稳 ...

  2. kotlin 类和对象_Kotlin程序| 类和对象的示例(带有学生数据)

    kotlin 类和对象 In the below program, we are creating a student class to input and print the student dat ...

  3. kotlin键值对数组_Kotlin程序以升序对数组进行排序

    kotlin键值对数组 Given an array, we have to sort its elements in ascending order. 给定一个数组,我们必须按升序对其元素进行排序. ...

  4. kotlin 两个数字相加_Kotlin程序交换两个数字

    kotlin 两个数字相加 Given two numbers, we have to swap them. 给定两个数字,我们必须交换它们. Example: 例: Input: First num ...

  5. kotlin数据类_Kotlin数据类

    kotlin数据类 In this tutorial, we'll look at Kotlin Data Class. If you haven't read the Kotlin Classes ...

  6. kotlin 构造函数_Kotlin程序| 主要构造函数示例

    kotlin 构造函数 主要建设者 (Primary Constructor) A Kotlin class have Primary constructor and one or more Seco ...

  7. kotlin半生对象_Kotlin程序| 随播对象特征

    kotlin半生对象 伴侣对象 (Companion object) If you need a function or a property to be tied to a class rather ...

  8. 编写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身,如

    **编写一个Java应用程序,该应用程序包括2个类:Print类和主类E.Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身 ...

  9. kotlin 编译时常量_Kotlin程序| 编译时常量示例

    kotlin 编译时常量 编译时常数 (Compile-time Constant) If the value of a read-only (immutable) property is known ...

最新文章

  1. 昇腾 OSError: [Errno 22] Invalid argument: ‘protocol‘
  2. Python 语言介绍
  3. Mysql 连接查询
  4. IntelliJ IDEA 15快捷键大全
  5. 二叉树的中序遍历非递归方法(算法导论第三版12.1-3)
  6. setcellvalue 格式_POI对Excel单元格的格式的设置参数
  7. Netbackup detected IBM drives as unusable
  8. java判断输入月份_Java输入年份和月份判断多少天实例代码
  9. 《python透明人士,他是凭什么成为主流编程的宠儿?!》Python 正则表达式
  10. concurrent模块的使用
  11. Intel的AVX2指令集解读
  12. the Disadvantages of an Elite Education
  13. 密度计算机公式,密度浓度换算公式(浓度和密度的换算关系)
  14. 简单创意思维导图绘制教程分享
  15. 基于Java的大型网站设计方案
  16. 完整iOS APP发布App Store上架流程指南
  17. C++标准库(第二版,作者_NicolaiMJosuttis)_第六章标准模板库_概述
  18. 计算机网络传输介质的特点,计算机网络基础:数据通信技术之传输介质
  19. 1046 mysql_mysql 1046 错误解决实例_MySQL
  20. R语言基础统计分析:正态性检验、方差齐性检验、T检验、方差分析、秩和检验

热门文章

  1. python垃圾回收机制为什么标记能解决循环引用问题_python 关于循环引用以及标记清除的问题...
  2. python 日志不会按照日期分割_django实现日志按日期分割
  3. Docker容器间网络通信
  4. Redis(十一):Redis特殊类型之Bitmap位图
  5. 锁底层之内存屏障与原语指令
  6. C# Collection was modified;enumeration operation may not execute
  7. Idea项目中常见错误及笔记(Old)
  8. ES6 学习笔记(一)let,const和解构赋值
  9. django 中静态文件项目加载问题
  10. 浮动元素的均匀分布和两端对齐