在Scala中,super关键字用于引用父类的成员。如果super提示缺少'.',那么可能是因为在使用super关键字时没有指定具体的成员。

例如,在子类中使用super关键字调用父类的构造函数,应该使用类似于super(arg1, arg2)的语法。如果忘记了在super关键字后面添加'.',就会导致缺少'.'的错误提示。

另外,如果在调用父类的成员方法时也出现了类似的错误提示,那么可能是因为没有在super关键字后面添加'.'和具体的方法名。

下面是一个示例,演示了如何正确使用super关键字:

class ParentClass(arg1: Int, arg2: String) {
  def method(): Unit = {
    println("Parent method")
  }
}

class ChildClass(arg1: Int, arg2: String) extends ParentClass(arg1, arg2) {
  override def method(): Unit = {
    super.method() // 调用父类的method方法
    println("Child method")
  }
}

val child = new ChildClass(1, "hello")
child.method()

输出结果为:

Parent method
Child method

在上面的示例中,ChildClass继承自ParentClass,并重写了method方法。在重写的方法中使用super.method()调用了父类的method方法。

Scala Super关键字使用错误:缺少'.'的解决方法

原文地址: http://www.cveoy.top/t/topic/hxF3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录