gpt4 book ai didi

c# - 相互依赖的通用类?

转载 作者:太空狗 更新时间:2023-10-29 23:31:05 25 4
gpt4 key购买 nike

这篇文章的底部是一个解决方案的示例,尽管该示例显然无效,因为它在继承时使用了 BaseNode 和 BaseEdge 而未提供类型。

我正在尝试创建一个抽象图类,其中节点类和边类也必须是抽象的。每个类的单独实现将添加方法和属性,以及从基类实现抽象方法。

我有一个现有的解决方案,它只对所有内容使用基本类型,但是我很快发现自己陷入了转换的困惑之中。

只有图形类必须公开,尽管其他图形类可以公开,因此涉及嵌套在图形类中的节点和边类的解决方案是可以接受的。

有没有什么方法可以构造它,使所有属性都属于正确的类型,而无需在系统中的任何地方进行昂贵的强制转换?更重要的是实现类不必在任何地方都强制转换,但在这种情况下性能绝对是一个问题(实际上是),所以我宁愿完全避免强制转换。

abstract class Graph<TNode, TEdge>
where TNode : BaseNode<TEdge>
where TEdge : BaseEdge<TNode>
{
TNode root;
List<TNode> nodes;
List<TEdge> edges;

public abstract float process();
}

abstract class BaseNode<TEdge>
// THIS HERE IS THE PROBLEM
where TEdge : BaseEdge
{
List<TEdge> inputs;

public List<TEdge> Inputs
{
get
{
return inputs;
}
}

public abstract float process();
}

abstract class BaseEdge<TNode>
// THIS HERE IS THE PROBLEM
where TNode : BaseNode
{
TNode from;
TNode to;

public TNode To
{
get
{
return to;
}
}

public TNode From
{
get
{
return from;
}
}

public abstract float process();
}

@Marceln 想看看我现有的实现,所以就在这里。没有泛型也是一样。

abstract class Graph
{
BaseNode root;
List<BaseNode> nodes;
List<BaseEdge> edges;

public abstract float process();
}

abstract class BaseNode
{
List<BaseEdge> inputs;

public List<BaseEdge> Inputs
{
get
{
return inputs;
}
}

public abstract float process();
}

abstract class BaseEdge
{
BaseNode from;
BaseNode to;

public BaseNode To
{
get
{
return to;
}
}

public BaseNode From
{
get
{
return from;
}
}

public abstract float process();
}

Node 的实现可能如下所示:

class ImplementedNode : BaseNode
{
public override float process()
{
foreach (ImplementedEdge edge in this.Inputs)
{
// Something
}
}

public bool getImplementationSpecificInfo()
{
return true;
}
}

最佳答案

如果您愿意放宽对 BaseEdgeBaseNode 的限制,那么您可以执行以下示例中的操作。

我已经引入了接口(interface) INodeIEdge 只是为了让任何使用它的人都不允许创建 BaseEdge 的具体子类code>BaseNode 任何类型。

public interface INode
{

}

public interface IEdge
{

}

public abstract class Graph<TNode, TEdge>
where TNode : BaseNode<TEdge>
where TEdge : BaseEdge<TNode>
{
public TNode Root { get; set; }
public List<TNode> Nodes { get; set; }
public List<TEdge> Edges { get; set; }
}

public abstract class BaseNode<TEdge> : INode where TEdge: IEdge
{
List<TEdge> inputs;

public List<TEdge> Inputs
{
get
{
return inputs;
}
}

public abstract float process();
}

public abstract class BaseEdge<TNode> : IEdge where TNode: INode
{
TNode from;
TNode to;

public TNode To
{
get
{
return to;
}
}

public TNode From
{
get
{
return from;
}
}

public abstract float process();
}


public class ConcreteNode : BaseNode<ConcreteEdge>
{
public override float process()
{
return 0;
}
}

public class ConcreteEdge : BaseEdge<ConcreteNode>
{
public override float process()
{
return 0;
}
}

public class ConcreteGraph : Graph<ConcreteNode, ConcreteEdge>
{

}

关于c# - 相互依赖的通用类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976112/

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