Swift:在参数化类中覆盖方法 [英] Swift: Method overriding in parameterized class

查看:159
本文介绍了Swift:在参数化类中覆盖方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Swift很新,但是我有一些面向对象编程的经验。我已经开始尝试在Swift中使用参数化类,并且在重载方法时遇到了一个奇怪的设计特性。如果我定义了以下类:

I'm very new to Swift but I have some experience with OO-programming. I've started to try and use parameterized classes in Swift and I have come across a strange design feature when overloading methods. If I define the following classes:

class ParameterClassA {
}

class ParameterClassB: ParameterClassA {
}

class WorkingClassA<T: ParameterClassA> {
    func someFunction(param: T) -> Void {
    }
}

class WorkingClassB: WorkingClassA<ParameterClassB> {
    override func someFunction(param: ParameterClassA) {
    }
}



然后代码编译好。然而,正如你会注意到的,我已经重载了通常使用参数类型的函数,在我的例子中它是 ParameterClassB ,并给它一个 ParameterClassA 。这应该如何工作?我知道这是不允许在Java中,我想知道如何解释类​​型参数。它可以是类型参数的类层次结构中的任何内容吗?

Then the code compiles fine. However, as you'll notice, I've overloaded the function that normally uses the parameter type, which in my example is ParameterClassB, and given it a parameter of type ParameterClassA. How is that supposed to work? I know that it's not allowed in Java, and I'm wondering how the type parameter is interpreted. Can it be anything from the class hierarchy of the type parameter?

另外请注意,如果我在<$ c中移除了类型参数约束:ParameterClassA ,问题就完全一样了$ C> WorkingClassA 。

Also note that the problem is exactly the same if I remove the type parameter constraint : ParameterClassA in WorkingClassA.

如果我删除了覆盖关键字,那么我得到一个编译器错误,要求我添加它。

If I remove the override keyword, then I get a compiler error requesting that I add it.

非常感谢您的任何解释!

Thanks a lot for any explanation!

推荐答案

它与泛型没有任何关系(你称之为参数化)。它与Swift中一个函数类型如何替代另一个函数类型有关。规则是函数类型对于它们的参数类型是逆变的

It has nothing at all to do with the generics (what you call "parameterized"). It has to do with how one function type is substitutable for another in Swift. The rules is that function types are contravariant with respect to their parameter types.

为了更清楚地看到它,它将有助于抛弃所有误导性的通用材料和重写的东西,而是直接集中在用另一个函数类型替换另一个函数类型的业务上:

To see this more clearly, it will help to throw away all the misleading generic stuff and the override stuff, and instead concentrate directly on the business of substituting one function type for another:

class A {}
class B:A {}
class C:B {}

func fA (x:A) {}
func fB (x:B) {}
func fC (x:C) {}

func f(_ fparam : B -> Void) {}

let result1 = f(fB) // ok
let result2 = f(fA) // ok!
// let result3 = f(fC) // not ok

我们预计传递给函数 f 作为它的第一个参数,类型为 B - > Void ,但是一个 A - >类型的函数。可以使用Void 来代替,其中A是B的超类。

We are expected to pass to the function f as its first parameter a function of type B -> Void, but a function of type A -> Void is acceptable instead, where A is superclass of B.

但是,类型为 C - > Void not 是可以接受的,其中C是B的子类。函数对它们的参数类型是逆变的,而不是协变的。

But a function of type C -> Void is not acceptable, where C is subclass of B. Functions are contravariant, not covariant, on their parameter types.

这篇关于Swift:在参数化类中覆盖方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆