相关疑难解决方法(0)

在Scala中实现'.clone'

我想.clone在Scala中弄清楚如何使用自己的对象.

这是一个模拟,所以可变状态是必须的,从而产生了克隆的全部需求.在向前移动模拟时间之前,我将克隆整个状态结构.

这是我目前的尝试:

abstract trait Cloneable[A] {
  // Seems we cannot declare the prototype of a copy constructor
  //protected def this(o: A)    // to be defined by the class itself

  def myClone= new A(this)
}

class S(var x: String) extends Cloneable[S] {
  def this(o:S)= this(o.x)    // for 'Cloneable'
  def toString= x
}

object TestX {
  val s1= new S("say, aaa")
  println( s1.myClone )
}
Run Code Online (Sandbox Code Playgroud)

一个.为什么上面没有编译.得到:

error: class type required but A found
  def myClone= new A(this)
                   ^

湾 …

scala

4
推荐指数
1
解决办法
6892
查看次数

标签 统计

scala ×1