如何在不了解实现的情况下在抽象类中创建对象?

发布于 2024-09-01 17:42:35 字数 980 浏览 8 评论 0原文

有没有办法实现下面我的库摘要中的“CreateNode”方法?或者这只能在库外部的客户端代码中完成?我当前收到错误“无法创建抽象类或接口‘ToplogyLibrary.AbstractNode’的实例”

public abstract class AbstractTopology<T>
{
    // Properties
    public Dictionary<T, AbstractNode<T>> Nodes { get; private set; }
    public List<AbstractRelationship<T>> Relationships { get; private set; }

    // Constructors
    protected AbstractTopology()
    {
        Nodes = new Dictionary<T, AbstractNode<T>>();
    }

    // Methods
    public AbstractNode<T> CreateNode()
    {
        var node = new AbstractNode<T>();  // ** Does not work **
        Nodes.Add(node.Key, node);
        }
    }
 }

    public abstract class AbstractNode<T>
    {
        public T Key { get; set; }
    }

    public abstract class AbstractRelationship<T>
    {
        public AbstractNode<T> Parent { get; set; }
        public AbstractNode<T> Child { get; set; }

    }

Is there a way to implement the "CreateNode" method in my library abstract below? Or can this only be done in client code outside the library? I current get the error "Cannot create an instance of the abstract class or interface 'ToplogyLibrary.AbstractNode"

public abstract class AbstractTopology<T>
{
    // Properties
    public Dictionary<T, AbstractNode<T>> Nodes { get; private set; }
    public List<AbstractRelationship<T>> Relationships { get; private set; }

    // Constructors
    protected AbstractTopology()
    {
        Nodes = new Dictionary<T, AbstractNode<T>>();
    }

    // Methods
    public AbstractNode<T> CreateNode()
    {
        var node = new AbstractNode<T>();  // ** Does not work **
        Nodes.Add(node.Key, node);
        }
    }
 }

    public abstract class AbstractNode<T>
    {
        public T Key { get; set; }
    }

    public abstract class AbstractRelationship<T>
    {
        public AbstractNode<T> Parent { get; set; }
        public AbstractNode<T> Child { get; set; }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若水般的淡然安静女子 2024-09-08 17:42:35

您无法创建抽象类的实例,这就是您收到错误的原因。

您可以做的是将 CreateNode 声明为抽象方法,并在任何后代类中实现它。

You cannot create an instance of an abstract class, which is why you are receiving the error.

What you could do instead is declare CreateNode as an abstract method, and implement it in any descendant classes.

太阳公公是暖光 2024-09-08 17:42:35

那么您想要创建什么具体的节点类?这也许应该留给具体的拓扑类吗?如果是这样,您可能希望使其抽象:

public abstract AbstractNode<T> CreateNode();

然后在具体拓扑类中提供具体实现。

或者,您也可以使您的类在节点类型中通用:

public abstract class AbstractTopology<TValue, TNode>
    where TNode : AbstractNode<TValue>, new()

然后您可以像这样实现 CreateNode:

public AbstractNode<T> CreateNode()
{
    var node = new TNode();
    Nodes.Add(node.Key, node);
    return node;   
}

就我个人而言,我对涉及这么多抽象类的设计有点怀疑,但也许您有充分的理由。

Well what concrete node class do you want to be created? Is this perhaps something which should be left up to the concrete topology class? If so, you might want to make it abstract:

public abstract AbstractNode<T> CreateNode();

Then provide a concrete implementation in the concrete topology class.

Alternatively, you could make your class generic in the node type as well:

public abstract class AbstractTopology<TValue, TNode>
    where TNode : AbstractNode<TValue>, new()

Then you could implement CreateNode like this:

public AbstractNode<T> CreateNode()
{
    var node = new TNode();
    Nodes.Add(node.Key, node);
    return node;   
}

Personally I get a little suspicious of designs involving this many abstract classes, but perhaps you've got good reasons.

无悔心 2024-09-08 17:42:35

定义一个受保护的纯抽象虚方法,CreateNode将调用该方法,子类返回一个新对象

CreateNodeImpl或其他东西。

define a protected pure abstract virtual method that CreateNode will call that the subclass return a new object

CreateNodeImpl or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文