Scala中的家族多态性它是如何工作的

时间:2014-12-17 12:19:14

标签: scala types polymorphism traits

您好我在下面的代码中遇到以下错误。你能解释一下这个错误的原因吗?

Error - - type mismatch;  found   : UpperClassFamily.Mother
required: StandardFamily.M     (which expands to)  StandardFamily.Mother

代码:

 object testworksheet {

  trait Family {
        type M <: Mother
        type F <: Father
        type C <: Child

        class Father ( val name: String ) {
            def kiss (m:M) =println ( "Showing signs of af fect ion towards " + m.name)
        }
        class Mother ( val name: String )
        class Child ( val name: String ) {
            def askForhelp (m:M) = println ( "Screeaaaaming at " + m.name)
        }
    }

    object UpperClassFamily extends Family {
      type F = Father ; type M = Mother ; type C = PoliteChild

      class Mother (name: String , val lastName : String ) extends super.Mother (name)
      class PoliteChild (name: String ) extends Child (name) {
        override def askForhelp (m:M) = println ( "Asking " + m.name + m. lastName + " for help" )
      }
    }

    object StandardFamily extends Family {
        type F = Father ; type M = Mother ; type C = Child
    }

    def assignFamily ( f : Family ) = ( )

    val father = new StandardFamily.Father("John")
    val upperClassMother = new UpperClassFamily .Mother ( "Dorthea I I I ","test" )
    father.kiss(upperClassMother)  //Error Location
}

0 个答案:

没有答案