Scala中的单位类型 Scala单元类型教程

标量单位类型 (Scala Unit type)

The Unit type in Scala is used as a return statement for a function when no value is to be returned. Unit type can be e compared to void data type of other programming languages like Java. It is a subclass of anytype trait and is used when nothing means to return by the function.

当不返回任何值时,Scala中Unit类型用作函数的返回语句。 可以将单元类型与Java等其他编程语言的void数据类型进行比较。 它是任何类型特征的子类,并且在没有任何意义由该函数返回时使用。

Program to illustrate the working of unit type in Scala

该程序说明了Scala中单位类型的工作方式

object MyClass {
      def printval(a: String): Unit = {
          println("Hello! "+a)
      }

      def main(args: Array[String]) {
        val a = printval("IncludeHelp")
        println("Value returned is "+a)
      }
   }

Output

输出量

Hello! IncludeHelp
Value returned is ()

Syntax to define a main() method with Unit type

使用单元类型定义main()方法的语法

    def main(args: Array[String]) : Unit = { 
    } 

Example of main() method with Unit type

单位类型的main()方法示例

object MyClass {
    def main(args: Array[String]): Unit = {
        println("Hello world!");
    }
}

Output

输出量

Hello world!


翻译自: https://www.includehelp.com/scala/unit-type-in-scala.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值