警告在子类中使用更有效的函数
Warn towards use of more efficient function in subclass
我想为图形实现一个 Scala 库。这个库还应该包含不同类型的树。
1 class 树扩展了 class 图
2 class 图有一个方法 getAllPrecursors(Node n) ,它 returns 你可以到达 n 的所有节点。
3 class 树有 getParent(Node n) 方法,returns 节点 n 的父节点(作为选项,None 标记根)
现在,如果有人为一棵树调用方法 "getAllPrecursors",我想显示一条警告,例如“树最多有 1 个前体,请改用 getParent”。
有什么办法吗?有没有更好的方法来构建这个库?树不应该是图的子class吗?
首先,不要担心性能,除非你能证明这段代码的性能对整体性能很重要:"Premature optimisation is the root of all evil"
但是您可以通过重载 getAllPrecursors
并将其标记为弃用来做您想做的事:
class graph {
def getAllPrecursors(n: Node): List[Node] = ...
}
class tree extends graph {
def getParent(n: Node) = ...
@deprecated("Use getParent rather that getAllPrecursors", "1.0")
override def getAllPrecursors(n: Node) = List(getParent(n))
}
如果您对 tree
类型的值使用 getAllPrecursors
而不是 graph
类型的值(即使该值实际上是一个实例),这将给出弃用警告tree
)
就设计而言,最好将 getAllPrecursors
和 getParent
作为 Node
(和 TreeNode
)上的方法而不是图表上的方法本身。
我想为图形实现一个 Scala 库。这个库还应该包含不同类型的树。
1 class 树扩展了 class 图
2 class 图有一个方法 getAllPrecursors(Node n) ,它 returns 你可以到达 n 的所有节点。
3 class 树有 getParent(Node n) 方法,returns 节点 n 的父节点(作为选项,None 标记根)
现在,如果有人为一棵树调用方法 "getAllPrecursors",我想显示一条警告,例如“树最多有 1 个前体,请改用 getParent”。
有什么办法吗?有没有更好的方法来构建这个库?树不应该是图的子class吗?
首先,不要担心性能,除非你能证明这段代码的性能对整体性能很重要:"Premature optimisation is the root of all evil"
但是您可以通过重载 getAllPrecursors
并将其标记为弃用来做您想做的事:
class graph {
def getAllPrecursors(n: Node): List[Node] = ...
}
class tree extends graph {
def getParent(n: Node) = ...
@deprecated("Use getParent rather that getAllPrecursors", "1.0")
override def getAllPrecursors(n: Node) = List(getParent(n))
}
如果您对 tree
类型的值使用 getAllPrecursors
而不是 graph
类型的值(即使该值实际上是一个实例),这将给出弃用警告tree
)
就设计而言,最好将 getAllPrecursors
和 getParent
作为 Node
(和 TreeNode
)上的方法而不是图表上的方法本身。