关于在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