Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的接口多态性#_C#_.net_Polymorphism - Fatal编程技术网

C# C语言中的接口多态性#

C# C语言中的接口多态性#,c#,.net,polymorphism,C#,.net,Polymorphism,我有这样一个代码: public interface INode { INode Parent { get; set; } // ...... } public interface ISpecificNode : INode { new ISpecificNode Parent { get; set; } // ...... } public class SpecificNode : ISpecificNode { ISpecificNode Pare

我有这样一个代码:

public interface INode
{
    INode Parent { get; set; }
    // ......
}

public interface ISpecificNode : INode
{
    new ISpecificNode Parent { get; set; }
    // ......
}

public class SpecificNode : ISpecificNode
{
    ISpecificNode Parent { get; set; }
    // ......
}
public interface INode<T> where T : INode<T>
{
    T Parent { get; set; }
}

public interface ISpecificNode : INode<ISpecificNode>
{
}

public class SpecificNode : ISpecificNode
{
    public ISpecificNode Parent { get; set; }
}
此代码给出了一个编译错误,因为未实现INode.Parent。但是,我不需要重复的父属性。

如何解决这个问题?

这不是像类那样的继承/重写-接口不继承其他接口,而是实现它们

这意味着实现
isSpecificNode
的任何内容也必须实现
INode

我建议你撤掉这条线

new ISpecificNode Parent { get; set; }

可能没有必要。或者,您可以在抽象基类上实现
INode
,用具体的
SpecificNode
类覆盖它,并使用属性隐藏来隐藏属性的
INode
版本。

我想您正在寻找类似这样的东西:

public interface INode
{
    INode Parent { get; set; }
    // ......
}

public interface ISpecificNode : INode
{
    new ISpecificNode Parent { get; set; }
    // ......
}

public class SpecificNode : ISpecificNode
{
    ISpecificNode Parent { get; set; }
    // ......
}
public interface INode<T> where T : INode<T>
{
    T Parent { get; set; }
}

public interface ISpecificNode : INode<ISpecificNode>
{
}

public class SpecificNode : ISpecificNode
{
    public ISpecificNode Parent { get; set; }
}
公共接口INode,其中T:INode
{
T父{get;set;}
}
公共接口isSpecificNode:INode
{
}
公共类特定节点:isSpecificNode
{
公共isSpecificNode父级{get;set;}
}

有必要。我更喜欢使用isSpecificNode.Parent,而不需要强制转换类型。或者,Allessandro的答案是一种更好、更高级的技术。