Đây là mã của tôi:Trình khởi tạo từ các kiểu generic sẽ không được kế thừa nhanh chóng?
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)
này không biên soạn và hét lên lỗi như vậy:
repl.swift:9:9: error: 'C' cannot be constructed because it has no accessible initializers
Các mã sau đây có thể được biên dịch.
public class A {
public init(n : Int) {
print("A")
}
}
public class B : A {
}
public class C : B {
}
let x = C(n: 123)
Không thể yêu cầu loại trình khởi tạo loại chung loại được chỉ định?
======== bổ sung Dưới =======
“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.
Và đây
“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.”
Khi nhìn vào mã đầu tiên, lớp con B
doesn 't xác định bất kỳ initializers được chỉ định, nó sẽ tự động kế thừa tất cả các superclass của nó được chỉ định initializers, những người từ A<Int>
. Nhưng thực sự nó đã không mà dường như có dây với tôi.
Cũng được quan sát ở đây: http://stackoverflow.com/questions/31040044/swift-generic-superclass-init-not-accesible-when-constructing-its-subclass. –
ai đó đã nộp một vé trong jira nhanh chóng https://bugs.swift.org/projects/SR/issues/SR-416 –
@AnthonyKong Vâng đó là tôi. – AntiMoron