В приведенном ниже коде я получаю эту ошибку:
class Animal needs to be abstract, since: it has 5 unimplemented members. /** As seen from class Animal, the
missing signatures are as follows. * For convenience, these are usable as stub implementations. */ def
favFood_=(x$1: Double): Unit = ??? def flyingType_=(x$1: scala.designpatterns.Strategy.Flys): Unit = ??? def
name_=(x$1: String): Unit = ??? def sound_=(x$1: String): Unit = ??? def speed_=(x$1: Double): Unit = ???
Если я инициализирую все переменные экземпляра класса Animal для _, тогда код правильно компилируется. Что означает эта ошибка?
package scala.designpatterns
/**
*
* Decoupling
* Encapsulating the concept or behaviour that varies, in this case the ability to fly
*
* Composition
* Instead of inheriting an ability through inheritence the class is composed with objects with the right abilit built in
* Composition allows to change the capabilites of objects at runtime
*/
object Strategy {
def main(args: Array[String]) {
var sparky = new Dog
var tweety = new Bird
println("Dog : " + sparky.tryToFly)
println("Bird : " + tweety.tryToFly)
}
trait Flys {
def fly: String
}
class ItFlys extends Flys {
def fly: String = {
"Flying High"
}
}
class CantFly extends Flys {
def fly: String = {
"I can't fly"
}
}
class Animal {
var name: String
var sound: String
var speed: Double
var favFood: Double
var flyingType: Flys
def tryToFly: String = {
this.flyingType.fly
}
def setFlyingAbility(newFlyType: Flys) = {
flyingType = newFlyType
}
def setSound(newSound: String) = {
sound = newSound
}
def setSpeed(newSpeed: Double) = {
speed = newSpeed
}
}
class Dog extends Animal {
def digHole = {
println("Dug a hole!")
}
setSound("Bark")
//Sets the fly trait polymorphically
flyingType = new CantFly
}
class Bird extends Animal {
setSound("Tweet")
//Sets the fly trait polymorphically
flyingType = new ItFlys
}
}