2013-08-26 9 views
10

Nel codice qui sotto ricevo questo errore:Errore: Animal classe ha bisogno di essere astratto, in quanto: ha 5 membri non attuate

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 = ??? 

Se io inizializzare tutte le variabili di Animal classe per esempio _ quindi il codice compila correttamente. Cosa significa questo errore?

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 
    } 

} 

risposta

23

È necessario inizializzare le variabili. Se non lo fai, Scala presume che tu stia scrivendo una classe astratta e una sottoclasse riempirà l'inizializzazione. (Il compilatore ti dirà se hai una sola variabile non inizializzata.)

Scrivere = _ fa in modo che Scala riempisca i valori predefiniti.

Il punto è quello di farti riflettere su ciò che accade quando qualcuno (ad esempio tu, dopo aver dimenticato che devi impostare prima le cose) chiama qualcosa che usa per es. il suono senza che sia stato impostato.

(In generale si dovrebbe almeno riflettere attentamente sul fatto che questo è il modo giusto per strutturare il codice; molti campi che richiedono l'inizializzazione prima dell'uso è sicuro, senza alcun meccanismo per far rispettare l'inizializzazione, sta chiedendo problemi.)

Problemi correlati