2017-07-19 50 views

回答

4

Class指的是一個名爲Class類型。 Class.type是指名爲Class的對象的類型。

考慮以下代碼爲例:

class Foo { 
    val x = 42 
} 
object Foo { 
    val y = 23 
} 

def f(foo: Foo) { 
    println(foo.x) 
    // The next line wouldn't work because the Foo class does not have a member y: 
    // println(foo.y) 
} 

def g(foo: Foo.type) { 
    println(foo.y) 
    // The next line wouldn't work because the Foo object does not have a member x: 
    println(foo.x) 
} 

val foo1 = new Foo 
val foo2 = new Foo 
f(foo1) 
f(foo2) 
// Does not work because the object Foo is not an instance of the class Foo: 
// f(Foo) 

g(Foo) 
// Does not work because g only accepts the object Foo: 
// g(foo1) 
+0

謝謝,這是我失蹤了。 – gurghet