我可以在 C# 中创建两个可互换的 类 或者对于这样的事情推荐的方法是什么?
Can I create two interchangeable classes in C# or what's the recommended way for something like this?
我有 2 个 类 来自 2 个项目:生产和测试。
BinaryTreeNode
- 来自基础项目,我无法更改。
TreeNode
- 来自测试项目,我可以更改。
我想在测试项目中互换使用这些 类,并且可以毫无问题地从一个转换到另一个(或者至少从 BinaryTreeNode 到 TreeNode)。我可以在 C# 中执行此操作吗?如果是,如何?因为如果我推导它是行不通的(创建为 BinaryTreeNode/base 的对象不能转换为 TreeNode/derived)。我不能使用 cast 运算符,因为相同类型的道具不起作用。有什么想法吗?
public class BinaryTreeNode {
public BinaryTreeNode(int key) {
this.Key = key;
this.Color = 0;
}
public int Key { get; set; }
public BinaryTreeNode Left { get; set; }
public BinaryTreeNode Right { get; set; }
public BinaryTreeNode Parent { get; set; }
/// <summary>
/// 0 = Red
/// 1 = Black
/// </summary>
public Color Color { get; set; }
/// <summary>
/// AVL Balance item
/// </summary>
public int Balance { get; set; }
}
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
您可以编写递归 ToTreeNode
函数,将所有值复制到 TreeNode
的新实例。
public static class Extensions
{
public static TreeNode ToTreeNode(this BinaryTreeNode binary)
{
var treeNode = new TreeNode(binary.Key);
treeNode.left = binary.Left?.ToTreeNode();
treeNode.right = binary.right?.ToTreeNode();
}
}
如果只使用 C# 4.0
很重要,则必须这样写:
public static class Extensions
{
public static TreeNode ToTreeNode(this BinaryTreeNode binary)
{
var treeNode = new TreeNode(binary.Key);
if (binary.Left != null)
treeNode.left = binary.Left.ToTreeNode();
if (binary.Right != null)
treeNode.right = binary.right.ToTreeNode();
}
}
更新 1
如果您真的想使用转换,您可以实现 C# 的 explicit operator
功能。 (我不知道措辞是否正确。:D)
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
public static explicit operator TreeNode(BinaryTreeNode b)
{
return b.ToTreeNode();
}
}
但是采用这种方法有几个缺点:
- 使用 node.ToTreeNode()
优于 (TreeNode)node
。
- 浏览代码更加困难。
- 您必须编辑现有的 TreeNode
class。所以你打破了Open-Close Principle
。
我有 2 个 类 来自 2 个项目:生产和测试。
BinaryTreeNode
- 来自基础项目,我无法更改。
TreeNode
- 来自测试项目,我可以更改。
我想在测试项目中互换使用这些 类,并且可以毫无问题地从一个转换到另一个(或者至少从 BinaryTreeNode 到 TreeNode)。我可以在 C# 中执行此操作吗?如果是,如何?因为如果我推导它是行不通的(创建为 BinaryTreeNode/base 的对象不能转换为 TreeNode/derived)。我不能使用 cast 运算符,因为相同类型的道具不起作用。有什么想法吗?
public class BinaryTreeNode {
public BinaryTreeNode(int key) {
this.Key = key;
this.Color = 0;
}
public int Key { get; set; }
public BinaryTreeNode Left { get; set; }
public BinaryTreeNode Right { get; set; }
public BinaryTreeNode Parent { get; set; }
/// <summary>
/// 0 = Red
/// 1 = Black
/// </summary>
public Color Color { get; set; }
/// <summary>
/// AVL Balance item
/// </summary>
public int Balance { get; set; }
}
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
您可以编写递归 ToTreeNode
函数,将所有值复制到 TreeNode
的新实例。
public static class Extensions
{
public static TreeNode ToTreeNode(this BinaryTreeNode binary)
{
var treeNode = new TreeNode(binary.Key);
treeNode.left = binary.Left?.ToTreeNode();
treeNode.right = binary.right?.ToTreeNode();
}
}
如果只使用 C# 4.0
很重要,则必须这样写:
public static class Extensions
{
public static TreeNode ToTreeNode(this BinaryTreeNode binary)
{
var treeNode = new TreeNode(binary.Key);
if (binary.Left != null)
treeNode.left = binary.Left.ToTreeNode();
if (binary.Right != null)
treeNode.right = binary.right.ToTreeNode();
}
}
更新 1
如果您真的想使用转换,您可以实现 C# 的 explicit operator
功能。 (我不知道措辞是否正确。:D)
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
public static explicit operator TreeNode(BinaryTreeNode b)
{
return b.ToTreeNode();
}
}
但是采用这种方法有几个缺点:
- 使用 node.ToTreeNode()
优于 (TreeNode)node
。
- 浏览代码更加困难。
- 您必须编辑现有的 TreeNode
class。所以你打破了Open-Close Principle
。