我在思考这里的6.3等效功能:
class Foo( val i: Int )
class RichFoo( f: Foo ) {
def prettyPrint = "Foo(" + i + ")"
}
implicit def enrichFoo( f: Foo ) = new RichFoo(f)
val foo = new Foo( 667 )
println( foo.prettyPrint ) // Outputs "Foo(667)"
您可以做到
class Class {
var method = () => println("Hey, a method (actually, a function bound to a var)")
}
val instance = new Class()
instance.method()
// Hey, a method (actually, a function bound to a var)
val new_method = () => println("New function")
instance.method = new_method
instance.method()
// New function
在创建实例之后,方法本身是无法更改的。