Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于在scala中使用this.type_Scala - Fatal编程技术网

关于在scala中使用this.type

关于在scala中使用this.type,scala,Scala,我定义了两个类: class A { def method1 = this } class B extends A { def method2 = this } val b = new B 然后我检查了b.method1的类型: scala> b.method1.getClass res29: Class[_ <: A] = class B 所以我必须这样定义A和B: class A { def method1: this.type = this } class B exte

我定义了两个类:

class A { def method1 = this }
class B extends A { def method2 = this }

val b = new B
然后我检查了b.method1的类型:

scala> b.method1.getClass
res29: Class[_ <: A] = class B
所以我必须这样定义A和B:

class A { def method1: this.type = this } 
class B extends A { def method2: this.type = this } 

val b = new B
现在我检查
b.method1
的类型:

scala> b.method1.getClass
res31: Class[_ <: B] = class B

我的问题是说
Class[\up>让我们把表达式
Class分开,这是什么意思[\ucode>.type
是一种单例类型,是Scala中的一个特殊概念。例如,
3.type
不是
Int
,而是
3.type
,一种只包含数字3的单例类型。很难确定,但看起来你认为
.type
意味着其他东西。
scala> b.method1.getClass
res31: Class[_ <: B] = class B
scala> b.method1.method2
res32: b.type = B@4ff3ac