scala案例类中的“_”是什么

时间:2017-09-29 08:26:54

标签: scala playframework

我只是在研究一个带有hibernate的Play / scala示例。我的案例课我发现了一些这样的事情......

class Buddy(first: String, last: String) {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Int = _

var firstName: String = first
var lastName: String  = last

def this() = this (null, null)

override def toString = id + " = " + firstName + " " + lastName 
}

任何人都可以解释这行“var id:Int = _”的含义。

“__”在本代码中的含义是什么。它与getter方法无关,我想在这种情况下我猜getter方法名称将是id _。

提前致谢...

2 个答案:

答案 0 :(得分:3)

“_”表示“默认值” 现在,对于不同的数据类型,默认值可能不同。例如

default is 0 for Int
default is 0.0 for double
default is null for reference types

等等

在你的情况下,值为0

答案 1 :(得分:0)

Here您对下划线的含义和一些用例有很好的解释。

我喜欢看到某种操作的通配符。

链接博客中的示例:

XMLHttpRequest cannot load https://mybucket-name.s3-ap-southeast-
1.amazonaws.com/assets/img/quotes.png. Response to preflight request doesn't 
pass access control check: No 'Access-Control-Allow-Origin' header is 
present on the requested resource. Origin 'http://localhost:8100' is 
therefore not allowed access. The response had HTTP status code 403.

Error: Network Failure
at XMLHttpRequest.<anonymous> (xhr.js:52)
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (ng_zone.js:227)
at t.invokeTask (polyfills.js:3)
at e.runTask (polyfills.js:3)
at XMLHttpRequest.invoke (polyfills.js:3)

另一个例子:

expr match {
  case List(1,_,_) => " a list with three element and the first element is 1"
  case List(_*)  => " a list with zero or more elements "
  case Map[_,_] => " matches a map with any key type and any value type "
  case _ =>
  }
相关问题