我正在尝试将ComputingMethod
作为(最好是不可变的)List
返回。我想我想在一个单独的构造函数中将它声明为一个空列表,但我不确定如何开始或之后去哪里。
我所拥有的代码片的简化版本是:
class MyComputingMethod extends ComputingMethod{
override def execute(o: Vector[Object]): ComputingMethod = {
val wc = List[Object]()
for(i<-o.indices)
wc::(o(i)) //put vector objects in vector
wc
}
}
ComputingMethod
是定义execute
方法的特征。我在另一个班级中宣布了Object
。所以我想返回ComputingMethod
个对象,但我需要ComputingMethod
成为List
。我甚至不确定List
是应该声明为通用还是Object
(或Vector
Object
的列表。
我尝试过的事情:
object MyComputingMethod {
val mcm = List[Object]() //declaring outside class
type ComputingMethod = List[Object] //also tried within class
val mc = new ComputingMethod().mcm
/*the above doesn't allow me to use any of the List operations,
although mc is of type VirtualMachine,
but the 'mcm' list doesn't seem connected */
}
class MyComputingMethod extends ComputingMethod{
val mcm = List() //declaring outside def
def mCm : MyComputingMethod = List() //def rather than val, shot in the dark
override def execute(o: Vector[Object): ComputingMethod = {
val wc = List[Object]()
for(i<-o.indices)
mCm.wc::(o(i)) //put vector objects in vector
mCm.wc //attempt to return ComputingMethod List
}
}
所以我认为我需要做的是为MyComputingMethod创建一个通用构造函数(和一个私有?),在其中初始化一个我可以用作MyComputingMethod对象的空列表,但我不确定如何做到这一点。 任何建议都非常感谢。