我想将以下 Java 代码转换为 Scala 代码:
Object method1(Object ... objArray) {
if (objArray instanceof MyClass1[]) {
Object resArray[] = new Object[objArray.length];
for (int i = 0; i < objArray.length; i++) {
resArray[i] = objArray[i].toString();
}
return resArray;
} else {
List<Object> resArray = new ArrayList<Object>();
for (int i = 0; i < objArray.length; i++) {
for (Object obj: scalar(objArray[i])) {
resArray.add(obj);
}
}
return resArray.toArray();
}
}
//..........
private static class MyClass1 {
private Object obj;
public MyClass1(Object obj) {
this.obj = obj;
}
@Override
public String toString() {
return obj.toString();
}
}
这是我所拥有的,它会导致错误:
def method1(objs: Any*): Array[_] = objs match {
case x: Array[MyClass1] => x.map(toString) // MyClass1 looks like
case x => x.map(method2).toArray
}
//...................
def method2(obj: Any): Array[_] = {....} //it's perfectly fine
class MyClass1 (obj: AnyRef) {
override def toString = obj.toString
}
错误:
1)pattern type is incompatible with expected type;
[error] found : Array[MyClass1]
[error] required: Any*
[error] case x: Array[MyClass1] => x.map(toString)
2)type mismatch;
[error] found : Array[MyClass1]
[error] required: Any*
[error] case x: Array[MyClass1] => x.map(toString)
3)could not find implicit value for evidence parameter of type ClassManifest[Array[_]]
[error] case x => x.map(method2).toArray
我该如何解决这个问题?