标量单位类型 (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