Swift do子类继承初始化器? [英] Swift do subclasses inherit initializers?

查看:120
本文介绍了Swift do子类继承初始化器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

class Parent {

    var foo: Int

    init(someNum: Int) {
        self.foo = someNum
    }
}

class Child: Parent {

}

var parent = Parent(someNum: 999)
println(parent.foo)                  // prints "999"

var child = Child(someNum: 3872)   
println(child.foo)                   // prints "3872"

In Apple的2014年WWDC中级Swift视频,他说默认情况下不会继承初始值设定项,除非它的存储属性有默认值。但是,子类 Child 中的 foo 没有默认值,但它明确地继承了 init (someNum:)方法。我是否理解错误的解释或是否有其他事情发生?

In Apple's 2014 WWDC "Intermediate Swift" video, he says that initializers aren't inherited by default unless there is a default value for it's stored properties. However there is no default value for foo in the subclass Child but it clearly inherits the init(someNum:) method. Am I understanding the explanation wrong or is there something else going on?

推荐答案

来自Apple文档:


默认情况下,子类不会继承其超类初始值设定项。
但是,如果
满足某些条件,则会自动继承超类初始值设定项。在实践中,这意味着你不需要在许多常见场景中编写初始化程序覆盖,并且每当
安全时,
可以用最小的努力继承你的超类初始化程序。

Subclasses do not inherit their superclass initializers by default. However, superclass initializers are automatically inherited if certain conditions are met. In practice, this means that you do not need to write initializer overrides in many common scenarios, and can inherit your superclass initializers with minimal effort whenever it is safe to do so.

假设您为
在子类中引入的任何新属性提供默认值,则适用以下两条规则:

Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:

规则1 如果您的子类没有定义任何指定的初始值设定项,则
会自动继承其所有超类指定的初始值设定项。

Rule 1 If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

规则2
如果您的子类提供了所有
超类指定初始化程序的实现 - 通过按
规则1继承它们,或者通过提供作为
定义的一部分的自定义实现 - 然后它会自动继承所有超类
便利初始化器。

Rule 2 If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.

这些规则即使你的子类也适用进一步增加c onvenience
initializers。

These rules apply even if your subclass adds further convenience initializers.

在您的情况下,您没有在 Child <中提供任何指定的初始化程序/ code>。因此,唯一一个指定的初始化程序已继承自 Parent

In your case, you did not provide any designated initializer in Child. Therefore, the only one designated initializer has been inherited from Parent.

如果修改 Child class to:

If you modify the Child class to:

class Child : Parent {
  var age : Int
  init (age : Int) {
     self.age = age
    super.init(foo: 10)
  }
}

您将看到通过调用其超类初始化程序无法创建 Child 类。
阅读更多内容。

You will see that you cannot create a Child class by calling its superclass initializer. Read more.

编辑:

上述规则适用于更加不同的情况。也许文档对你来说有点混乱,但让我们停下来想一会儿。你有一个继承自超类的子类,它有一个属性 foo 。现在,如果您的子类没有添加任何属性,或者添加了带有默认值的属性,则不需要与超类中的初始化程序不同的初始化程序,因此您可以安全地使用它。另一方面,如果添加没有默认值的属性,则无法使用超类中的初始化程序,因为添加的属性需要以某种方式初始化。编译器会抱怨,你必须做以下两件事之一:

The rules above apply to a more different scenario. Maybe the docs are a little bit confusing for you, but let's just pause and think for a moment. You have a subclass which inherits from a superclass which has one property foo. Now, if your subclass does not add any properties, or adds properties with default values, there is no need for a different initializer than the one from the superclass and so you can safely use it. On the other hand, if you add a property without a default value, you cannot use the initializer from the superclass because the added property needs to be initialized somehow. The compiler will complain and you will have to do one of the two things:


  1. 你应该在子类中实现一个指定的初始化器,它将分配新属性并且还调用超类中的指定初始化程序。

  2. 您可以向属性添加默认值,但仍然可以使用超类中指定的初始化程序。

这篇关于Swift do子类继承初始化器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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