我需要的:
trait Base {
import scala.math.{Pi=>mPi, cos=>msoc, sin=>msin}
static val Pi : Float = mPi.toFloat
static def cos(phi : Float) : Float = mcos(phi).toFloat
static def sin(phi : Float) : Float = msin(phi).toFloat
// ...
// some useful functions
}
// children use float defined pi, cos and sin without additional boilerplate
class Child1 extends Base
// ...
class Child2 extends Base
那显然行不通。我试过的
trait Base {
import Base._
// some useful functions
}
object Base {
// utility functions
import scala.math.{Pi=>mPi, cos=>mcos, sin=>msin}
val Pi : Float = mPi.toFloat
def cos(phi : Float) : Float = mcos(phi).toFloat
def sin(phi : Float) : Float = msin(phi).toFloat
}
class Child1 extends Base {
// use sin and cos inside
}
并且编译器会给出错误,Child1
因为它对函数sin
和cos
.
如何在将由其后代继承的基类中定义静态成员?