无法在T中访问类Object中的Scala方法克隆,因为不允许访问受保护的方法克隆,因为 [英] Scala method clone in class Object cannot be accessed in T Access to protected method clone not permitted because
本文介绍了无法在T中访问类Object中的Scala方法克隆,因为不允许访问受保护的方法克隆,因为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Scala中克隆时遇到了麻烦。是否可以克隆任意类型T的对象?像这样的东西:
I've got troubles with cloning in Scala. Is this possible to clone an object of an arbitrary type T? Something like this:
import collection.immutable.Stack
object Tester extends App {
trait Grand[T <: Cloneable] {
val stack = Stack[T]()
val h: T
def snapshot() {
stack push h.clone().asInstanceOf[T]
}
}
}
但是它抛出:
scala:类对象中的方法克隆不能在T中访问
不允许访问受保护的方法克隆,因为
前缀类型T不符合
特性发生访问的对象测试器中的盛大
那里出了什么问题?
推荐答案
有人告诉我这个问题。在这种情况下,最好的方法是使用结构化类型:
I was advised on this question. In such a situation the best approach is to use structural typing:
trait Grand[T <: {def cloneObject: T}]
,以便用户代码如下:
case class Person(name: String) {
def cloneObject = copy()
}
object Roll extends App with Grand[Person] {
...
}
这篇关于无法在T中访问类Object中的Scala方法克隆,因为不允许访问受保护的方法克隆,因为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文