加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – “new”关键字不适用于不可变队列

发布时间:2020-12-16 09:49:53 所属栏目:安全 来源:网络整理
导读:我尝试用新的Keyword创建一个Queue ..我为可变和不可变的Queue做了它. 但是当我尝试使用不可变队列时,它会出错: console:8: error: constructor Queue in class Queue cannot be accessed in object $iw Access to protected constructor Queue not permitt
我尝试用新的Keyword创建一个Queue ..我为可变和不可变的Queue做了它.

但是当我尝试使用不可变队列时,它会出错:

<console>:8: error: constructor Queue in class Queue cannot be accessed in object $iw
 Access to protected constructor Queue not permitted because
 enclosing class object $iw in object $iw is not a subclass of 
 class Queue in package immutable where target is defined
       val a=new Queue[Int]()
             ^

scala> import scala.collection.immutable.Queue
import scala.collection.immutable.Queue

scala> val a=new Queue[Int]()
<console>:8: error: constructor Queue in class Queue cannot be accessed in object $iw
 Access to protected constructor Queue not permitted because
 enclosing class object $iw in object $iw is not a subclass of 
 class Queue in package immutable where target is defined
       val a=new Queue[Int]()

但是当我尝试使用可变队列,不可变堆栈,可变堆栈的代码时…它运行良好

scala> import scala.collection.mutable.Queue
import scala.collection.mutable.Queue

scala> val a=new Queue[Int]()
a: scala.collection.mutable.Queue[Int] = Queue()



scala> import scala.collection.immutable.Stack
import scala.collection.immutable.Stack

scala> val a=new Stack[Int]()
a: scala.collection.immutable.Stack[Int] = Stack()


scala> import scala.collection.mutable.Stack
import scala.collection.mutable.Stack

scala> val a=new Stack[Int]()
a: scala.collection.mutable.Stack[Int] = Stack()

谁能告诉我,为什么会这样?

解决方法

从快速浏览一下 sourcecode,我冒昧地说,不可变版本基本上是通过几个列表来实现的,这些列表根据需要进行调整,以便在读取和写入(队列/出队)之间提供良好的性能.

这与其他集合完全不同,因为它需要两个提到的列表作为类构造函数的参数.
另一方面,伴随对象通过接受Queue内容的可变数量的初始值,提供与其他集合一致的公共工厂方法.

缺少的是类的公共构造函数,它将模仿伴随对象应用调用,获取初始值,并使用它们构建“enque / dequeue”列表.

也许这不是必要的,或者它是一种疏忽,或者是一个我无法弄清楚的更深层次的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读