首页 > 解决方案 > 如何扩展一个扩展 App 的类

问题描述

我有一个主基类,我想扩展它,以便主类为扩展类提供基本功能:

class Foo extends App {
  println("Foo")
  def bar = {}
}

class Foo2 extends Foo {
  println("Foo2")
}

object Foo2 extends Foo2 {

}

但这不起作用。编译抱怨warning: Foo2 has a main method with parameter type Array[String], but Foo2 will not be a runnable program. Reason: companion contains its own main method, which means no static forwarder can be generated.

如何做到这一点?

标签: scala

解决方案


There is an ambiguous name in your code. Just rename the class or object and you'll be fine

class Foo extends App {
  println("Foo")
  def bar = {}
}

class FooX extends Foo {
  println("Foo2")
}

object Foo2 extends FooX {

}

推荐阅读