有“自我”吗?输入代表当前类型的scala? [英] Is there a "SELF" type in scala that represents the current type?

查看:71
本文介绍了有“自我”吗?输入代表当前类型的scala?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Scala并且有一些我无法找到关于语言的事情:

I'm learning Scala and there is a thing that I can't find out about the language:

前段时间我在Lisaac编程非常舒服,在Lisaac中,我可以写一个 PERSON 的类,其中有一个插槽列表:ARRAY [SELF] ,相当于有 list:ARRAY [PERSON] ,因为 SELF 是该广告位所在的对象的类型。

Some time ago I was very comfortable programming in Lisaac, and in Lisaac I could write a class PERSON with a slot list:ARRAY[SELF], which was equivalent to have list:ARRAY[PERSON], since SELF is the type of the object where that slot is.

但是通过使用 SELF ,如果我写第二个班级 STUDENT 那个继承自 PERSON ,然后 STUDENT 将继承该插槽更改 SELF 对于学生,所以学生将有一个学生而不是 PERSON

But by using SELF, if I write a second class STUDENT that inherits from PERSON, then STUDENT would inherit that slot changing SELF for STUDENT, so STUDENT would have a list of STUDENT instead of PERSON.

可以在Scala中完成吗?我无法找到任何相关内容。

Can that be done in Scala? I can´t find out anything about that.

谢谢!

推荐答案

<我不确定这对你是否真的有用,但我能想到的最接近的是 this.type 。例如:

scala> class A { val l: List[this.type] = Nil }  
defined class A

scala> new A().l
res3: List[A] = List()

scala> class B extends A
defined class B

scala> new B().l
res4: List[B] = List()

这篇关于有“自我”吗?输入代表当前类型的scala?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆