泛型类型的初始化器不会被快速继承吗?

问题描述:

这里是我的代码:泛型类型的初始化器不会被快速继承吗?

public class A<T : Any> { 
    public init(n : Int) { 
     print("A") 
    } 
} 
public class B : A<Int> { 
} 
public class C : B { 
} 
let x = C(n: 123) 

这种失败的编译和破口大骂这样的错误:

repl.swift:9:9: error: 'C' cannot be constructed because it has no accessible initializers 

下面的代码可以编译。

public class A { 
    public init(n : Int) { 
     print("A") 
    } 
} 
public class B : A { 
} 
public class C : B { 
} 
let x = C(n: 123) 

如果没有指定泛型类型初始化需求类型继承?

========以下其他=======

“Superclass initializers are inherited in certain circumstances, but only when it is safe and appropriate to do so. For more information, see Automatic Initializer Inheritance below.”
---Apple Inc. “The Swift Programming Language (Swift 2)” iBooks.

“However, superclass initializers are automatically inherited if certain conditions are met.”

“Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:” Rule 1 “If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.” 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.”

当寻找到的第一个代码,子类B没有按没有定义任何指定的初始化器,它应该自动继承它的所有超类指定的初始化器,那些来自A<Int>的初始化器。但实际上它并没有与我连接。

+1

此处还观察到:http://stackoverflow.com/questions/31040044/swift-generic-superclass-init-not-accesible-when-constructing-its-subclass。 –

+0

某人已经在swift jira上提交了一张票https://bugs.swift.org/projects/SR/issues/SR-416 –

+0

@AnthonyKong是的,那就是我。 – AntiMoron

那怎么样?我尝试使用override代码和super.init,这不是错误。我认为你不必使用泛型类型来初始化函数。

试图把控初始化函数在B类和C类 这个样子,

public override init(n:Int) { super.init(n: n) }

发生故障的代码编译现在(因为斯威夫特3)。在Swift 3 Language changes中没有提及此更改,所以我只能假设这是一个错误。