[英]Unable to create companion class instance in companion object method
What's wrong with this code: 这段代码有什么问题:
class Trivials(s:String){
private val x = 0
}
object Trivials {
def main(args: Array[String]): Unit = {
Trivials t = new Trivials("Trivials")
}
}
Both class and object are defined in same source file, hence they are companion. 类和对象都在同一源文件中定义,因此它们是同伴。
Error message is as: 'Cannot resolve symbol t' 错误消息为:'无法解析符号t'
Wrong syntax (You are using Java syntax) for object creation. 用于对象创建的语法错误(您正在使用Java语法)。 In case of Scala you need not mention the type in front of the variable
t
it will be automatically inferred. 对于Scala,您无需在变量
t
前面提到类型,它会自动推断出来。
Trivials t = new Trivials("Trivials")
Scala syntax Scala语法
val t = new Trivials("Trivials")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.