有没有办法让一个方法总是返回调用它的同一个类的类型?
让我解释:
class Shape {
var mName: String = null
def named(name: String): Shape = {
mName = name
this
}
}
class Rectangle extends Shape {
override def named(name: String): Rectangle = {
super.named(name)
this
}
}
这可行,但是有没有办法做到这一点而不必覆盖named
我所有子类中的函数?我正在寻找这样的东西(不起作用):
class Shape {
var mName: String = null
def named(name: String): classOf[this] = { // Does not work but would be great
mName = name
this
}
}
class Rectangle extends Shape {
}
任何的想法 ?还是不可能?