这里还有两点:
- A function and a method are not the same thing
- Methods are not polymorphic in their parameters types
页: 1 方法如下:而不是Function1
>。 它可以使用 underscore子解除其职能:
val f = (new FooTest[String]).tester _ // Fasel => Bla
这一职能在投入类别中将具有反常性。 (然而,值得指出的是,职能不能算作准<>m>,也值得说的是,我必须有一个<代码>的例子。 Foo或Foo 试验
,以便获得<代码>te方法的功能标语。 当然,这是第一次观察之后的。
一项功能是一个目标,不能凌驾于之上,因为这毫无意义。 方法可以压倒一切。 然而,正如我前面所说,在方法参数类型中,压倒性并非多变。 例如:
class A {
def foo(a : Any) = println("A: " + a)
}
class B extends A {
override def foo(s : String) = println("B " + s) //will not compile!
}
我的上述两种方法是两种不同的方法:动态发送只针对方法目标(即所呼吁的目标)。
例如,如果你删除<代码>overe的声明,则该守则将汇编成册。 如果是:
(new B).foo(1) //prints A 1
(new B).foo("s") //prints B s
This is because, although both methods are called foo
, they are completely different methods (i.e. I have overloaded foo
, not overridden it). It s best understood as being that a method s arguments (incl their types) form part of that method s unique name. One method overrides another only if they have exactly the same name.
基本上,你将你提出的问题中的两点内容()混为一谈,我将澄清:
- The variance annotations on
Function1
define what it means for one function to be a subtype of another (and hence assignable to a reference of a given type).
- Methods can be overridden on subclasses and the language specification outlines rules for when such overriding takes place.