kotlin 扩展类的功能_Kotlin程序| 扩展功能功能

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 扩展类的功能

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544135.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

聊聊保证线程安全的10个小技巧

前言对于从事后端开发的同学来说,线程安全问题是我们每天都需要考虑的问题。线程安全问题通俗的讲:主要是在多线程的环境下,不同线程同时读和写公共资源(临界资源),导致的数据异常问题。比如:变…

Raid控制器

转载于:https://blog.51cto.com/xuepengdou/1699799

并行计算机架构_计算机科学组织| 并行处理

并行计算机架构并行处理 (Parallel Processing) Parallel processing is processing of the data concurrently. We process the data concurrently to fulfill the demands of the increasingly high performance so that to achieve better throughput instead of processing…

15个必知的Mysql索引失效场景,别再踩坑了!

背景 无论你是技术大佬,还是刚入行的小白,时不时都会踩到Mysql数据库不走索引的坑。常见的现象就是:明明在字段上添加了索引,但却并未生效。前些天就遇到一个稍微特殊的场景,同一条SQL语句,在某些参数下生效…

网站高可用数据

网站高可用数据1、CAP原理:数据一致性,数据可用性,分区耐受性,无法同时满足2、数据备份a、冷备份b、热备份1、同步热备份2、异步热备份3、失效转移a、失效确认b、访问转移c、数据恢复转载于:https://blog.51cto.com/hanchengen/17…

运维工程师常见软件故障_软件故障 软件工程师

运维工程师常见软件故障软件故障 (Software Failure) A failure that shows up when the user recognizes that the software has come to bring to an end to deliver the anticipated result with respect to the specification input values. The user may need to identify…

干掉 Swagger UI,这款神器更好用、更高效!

事情是这样的:今天我们公司的后端说他接口写完了,并分享了一个接口文档给我。用的就是 Swagger UI 自动生成的那种接口文档,就像这种:这种 Swagger UI文档我每次看着就头大,毛病多多查看多级模型时要一级级点开在接口数…

Android UI ActionBar功能-ActionBarProvider的使用

分享功能是很多App都有一个功能,ActionBarProvider可以实现分享功能: 3.0以前的版 本和3.0以后的版 本的区别: public class MainActivity extends Activity {private ShareActionProvider provider;Overrideprotected void onCreate(Bundle …

ruby 类方法与实例方法_Ruby Set相交? 实例方法

ruby 类方法与实例方法Ruby Set相交? 方法 (Ruby Set intersect? Method) intersect?(Set) method is a method which is predefined in Rubys library. You may call this method as an exact opposite of Set.disjoint?() method. With the help of this metho…

面渣逆袭:MyBatis连环20问,这谁顶得住?

大家好,今天我们的主角是MyBatis,作为当前国内最流行的ORM框架,是我们这些crud选手最趁手的工具,赶紧来看看面试都会问哪些问题吧。基础1.说说什么是MyBatis?MyBatis logo先吹一下:Mybatis 是一个半 ORM(对…

Wi-Fi 协议和数率?

IEEE 802.11Wi-Fi 协议摘要 协议 频率 信号 最大数据速率 传统 802.11 2.4GHz FHSS 或 DSSS 2Mbps 802.11A 5GHz OFDM 54Mbps 802.11B 2.4GHz 人力资源DSSS 11Mbps 802.11G 2.4GHz OFDM 54Mbps 802.11n 2.4 或 5GHz OFDM 600Mbps(理论值) 802.11AC 5…

as_hash ruby_Ruby中带有示例的Hash.keep_if方法

as_hash rubyHash.keep_if方法 (Hash.keep_if Method) In this article, we will study about Hash.keep_if Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method wit…

高并发下如何防重?

前言最近测试给我提了一个bug,说我之前提供的一个批量复制商品的接口,产生了重复的商品数据。追查原因之后发现,这个事情没想象中简单,可以说一波多折。1. 需求产品有个需求:用户选择一些品牌,点击确定按钮…

Virtualbox中win7虚拟机中U盘不可用问题的解决

Virtualbox版本是5.0.0,主机运行多是Ubuntu12.04 LTS,虚拟机是Win7 X64。起初Win7正常运行,Virtualbox的增强功能已安装。下面是如何一步一步解决U盘不可用的详细过程。 1. 直接插入U盘,Win7下无任何反映 Virtualbox管理器中&…

面试突击55:delete、drop、truncate有什么区别?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)在 MySQL 中,删除的方法总共有 3 种:delete、truncate、drop,而三者的用法和使用…

python中八进制_在Python中以八进制格式输入数字

python中八进制Syntax to convert octal value to an integer (decimal format), 将八进制值转换为整数(十进制格式)的语法, int(oct_value, 8)Here, 这里, oct_value should contain the valid octal value oct_value应该包含有效的八进制值 8 is the…

Ruby file操作cheatsheet

每次都要查,真是蛋疼,不如一次性总结一下,以后再不记得就来这里找好了。以下代码中需要用到的文件名:filename ‘testfile.txt’ 读取其中的全部内容:File.read(filename)将一个字符串一次性写入这个文件:…

大厂也在用的 6种 数据脱敏方案,别做泄密内鬼

最近连着几天晚上在家总是接到一些奇奇怪怪的电话,“哥,你是 xxx 吧,我们这里是 xxx 高端男士私人会所...”,握草,我先是一愣,然后狠狠的骂了回去。一脸傲娇的转过头,面带微笑稍显谄媚&#xff…

在Python中使用OpenCV裁剪图像

What is Cropping? 什么是播种? Cropping is the removal of unwanted outer areas from a photographic or illustrated image. The process usually consists of the removal of some of the peripheral areas of an image to remove extraneous trash from the…

面渣逆袭:RocketMQ二十三问

1.为什么要使用消息队列呢?消息队列主要有三大用途,我们拿一个电商系统的下单举例:解耦:引入消息队列之前,下单完成之后,需要订单服务去调用库存服务减库存,调用营销服务加营销数据……引入消息…