5.Unit、Nothing、Any的区别

本文详细介绍了Kotlin中的Unit和Nothing类型。Unit类似于Java的void,表示无返回值的函数。而Nothing则表示没有任何值,常用于表示函数不会返回的情况,如抛出异常。在类型系统中,Any?位于顶部,Nothing位于底部。通过示例展示了如何使用这两个类型,并讨论了它们在函数返回类型中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:Nothing

 在Kotlin类型层次结构的最底层就是类型 Nothing


 二:Unit与Nothing之间的区别:

Unit类型表达式计算结果的返回类型是Unit。

Nothing类型的表达式计算结果是永远不会返回的(跟Java 中的 void 相同)。

 Nothing是没有实例的。您可以使用Nothing来表示“一个不存在的值

如果一个函数的返回类型为Nothing,这意味着它永远不会返回(总是抛出异常)

Nothing? 它唯一允许的值是 null ,被用作任何可空类型的空引用。

Unit类型表达式计算结果返回值是Unit;Unit类型实现了与java中void一样的功能。

Java语言的void关键字(void是 无参数返回的  )   他是关键字啊,不是类型

 Unit不写可省略,默认也有,Unit代表  无参数返回的 忽略类型(Unit类型类)

当一个函数没有返回值的时候,我们用Unit来表示这个特征,而不是null,大多数时候我们不需要显示地返回Unit,


//Unit : 无返回值,可省略
// Kotlin 中Unit类型实现了与java中void一样的功能。
fun test11(): Unit {}    //kotlin.Unit
fun test12() {}          //kotlin.Unit
 fun unitExample():kotlin.Unit {
        println("test,Unit")
    }
    fun unitExample(){
        println("test,Unit")
        return kotlin.Unit
    }
    fun unitExample(){
        println("test,Unit")
    }

三:Any? 是整个类型体系的顶部, Nothing 是底部


fun main(args: Array<String>) {
    fail("Error occurred")
}

fun fail(message:String):Nothing{
    throw IllegalStateException(message)
}

在Ubuntu 22.04中,如果你想要将一个程序添加到开机启动中,你可以通过修改rc.local文件来实现。在Ubuntu中,有两个rc.local文件,分别是/etc/rc.local和/etc/init.d/rc.local。这两个文件有一定的关联关系。 如果你在系统中找不到这两个文件,你可以自己创建一个rc-local.service文件,并在其中添加以下内容: [Unit] Description=/etc/rc.local Compatibility ConditionPathExists=/etc/rc.local [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 StandardOutput=tty RemainAfterExit=yes SysVStartPriority=99 [Install] WantedBy=multi-user.target 然后,你可以启动rc-local.service服务并将其设置为开机自启: sudo systemctl start rc-local.service sudo systemctl enable rc-local.service 最后,你可以创建/etc/rc.local文件,并将以下内容复制粘贴到文件中: #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. exit 0 记得给予脚本执行权限。通过以上步骤,你可以将程序添加到Ubuntu 22.04的rc.local中,并实现开机启动。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ubuntu系统中/etc/rc.local和/etc/init.d/rc.local的区别详解](https://download.csdn.net/download/weixin_38564826/14890316)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ubuntu没有rc.local文件](https://blog.csdn.net/qq_35002542/article/details/127999561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值