gpt4 book ai didi

swift - 子类化问题: dictionary values from a subclass?

转载 作者:行者123 更新时间:2023-11-30 10:46:22 25 4
gpt4 key购买 nike

短篇故事:

我试图创建一个类,其子类可以有一个更专门的成员(字典),然后我遇到了以下错误:

error: property 'dict' with type '[Int : B]' cannot override a property with type '[Int : A]'

这是我的代码:

class A {}
class B: A {} // subclass of A

let dict = [Int:B]()
print(dict is [Int:A]) // true

class MyDict {
var dict = [Int:A]()
}

class MyDict2: MyDict {

/*
⛔ error:
property 'dict' with type '[Int : B]' cannot override a property with type '[Int : A]'
*/
override var dict = [Int:B]()

}

我的问题是,既然 [Int:B] 的实例是 [Int:A],为什么我们不能覆盖 类型的字典>[Int:A][Int:B]

长话短说:

实际上,我试图为加权图和未加权图设计类型。以下是我的实现:

// unweighted edge
public class Edge<Vertex:Hashable> {
public let start: Vertex
public let end : Vertex
init(from start: Vertex, to end: Vertex) {
self.start = start
self.end = end
}
}

// weighted edge
public class WeightedEdge<Vertex:Hashable, Weight:Comparable> : Edge<Vertex> {
public let weight: Weight
init(from start:Vertex, to end:Vertex, weight:Weight) {
self.weight = weight
super.init(from:start, to:end)
}
}

// unweighted graph (definition)
public class Graph<Vertex:Hashable> {

// edges dictionary
var edgesOf = [Vertex: [Edge<Vertex>]]()

// g.addEdge(from:to:bidirectional:)
public func addEdge(
from start: Vertex,
to end:Vertex,
bidirectional:Bool = false
) {
edgesOf[start, default:[]].append(Edge(from: start, to:end))
if bidirectional {
edgesOf[end, default:[]].append(Edge(from: end, to:start))
}
}

}

// weighted graph (definition)
public class WeightedGraph<Vertex:Hashable, Weight:Comparable> : Graph<Vertex> {

public override func addEdge(from start:Vertex, to end:Vertex, bidirectional:Bool = false) {
fatalError("Can't add an unweighted edge to a weighted graph❗")
}

// g.addEdge(from:to:weight:bidirectional:)
public func addEdge(
from start: Vertex,
to end: Vertex,
weight : Weight,
bidirectional: Bool = false
) {
edgesOf[start, default:[]].append(
WeightedEdge(from: start, to:end, weight:weight)
)
if bidirectional {
edgesOf[end, default:[]].append(
WeightedEdge(from: end, to:start, weight:weight)
)
}
}

}

let g = WeightedGraph<Int,Int>()
g.addEdge(from: 3, to: 4, weight:7, bidirectional: true)
g.addEdge(from: 1, to: 3) // fatalError

它对我来说效果很好,但我对它丑陋的部分感到困扰:

public override func addEdge(from start:Vertex, to end:Vertex, bidirectional:Bool = false) {
fatalError("Can't add an unweighted edge to a weighted graph❗")
}

如何防止加权图向自身添加未加权边?

最佳答案

当您继承一个类时,您将在其中包含父类(super class),因此您会得到同一属性的冲突定义,稍微简化了 MyDict2 的编译版本

class MyDict2 {
var dict = [Int:A]()
var dict = [Int:B]()
}

正如你所看到的,这是行不通的。或者,dict 属性将被替换,但随后您会遇到一个问题,即任何接受 MyDict 类型参数的函数也可以采用 MyDict2 的实例> 但如果该函数访问 dict 属性,它将找到 B 类型的值,并且该函数将不知道是什么,因为它被定义为采用 MyDict 因此只知道 A

我不认为继承是这里的前进方向,替代方案是拥有两个符合相同协议(protocol)的独立类来访问底层存储(字典)或使用泛型实现它

class MyDict<T> {
var dict = [Int:T]()
}

let c1 = MyDict<A>()
let c2 = MyDict<B>()

关于swift - 子类化问题: dictionary values from a subclass?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55699458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com