![](/img/trans.png)
[英]type definition in a package object “hijacking” the inheritance of a java class in scala code
[英]Inheritance with Scala 'object'
我有这个 Java 代码:
class Super {
public static void foo() { bar(); }
public static void bar() { out.println("BAR");}
public static void main(String[] args) {
foo();
}
}
class Sub extends Super {
public static void bar() { out.println("bar"); }
}
而且我想看看它在 Scala 中做了什么,但似乎找不到如何编写等价物。 这就是我所拥有的:
object Super {
def foo() { bar() }
def bar() { println("BAR")}
def main( args : Array[String]) {
foo()
}
}
object Sub extends Super {
override def bar() { println("bar")}
}
但不编译。 是因为object不能继承?
你可能想把它改成这样
class Super {
def foo() { bar() }
def bar() { println("BAR")}
def main( args : Array[String]) {
foo()
}
}
object Super extends Super {
}
object Sub extends Super {
override def bar() { println("bar")}
}
这样你就有了 Super 类型和 object Super 类型。
编辑:刚刚将主要方法移至 class Super,这样它就更干净了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.