无法通过接口的保护集实现属性

发布于 2025-01-23 04:39:10 字数 886 浏览 3 评论 0原文

我有一个界面,该界面具有带有公共获取和受保护集的属性。

但是,当我在课堂上实施此功能时,我会发现一个错误,说它必须公开。

我的界面看起来像这样:

public interface ISegment
{
    INode NodeA { get; protected set; }
    INode NodeB { get; protected set; }
    public sealed void SetNodeA(INode node) => NodeA = node;
    public sealed void SetNodeB(INode node) => NodeB = node;
}

我的类段:Isegment是否属于这样的属性:

[SerializeField]
protected Node _nodeA;
public INode NodeA
{
    get => _nodeA;
    protected set => _nodeA = value as Node;
}

[SerializeField]
protected Node _nodeB;
public INode NodeB
{
    get => _nodeB;
    protected set => _nodeB = value as Node;
}

我得到了此错误:

'Segment' does not implement interface member 'ISegment.NodeA.set'
'Segment' does not implement interface member 'ISegment.NodeB.set'

我在这里误会了什么?

I have an interface which has a property with public get and protected set.

Yet when I implement this in my class i get an error saying it must be public.

My interface looks like this:

public interface ISegment
{
    INode NodeA { get; protected set; }
    INode NodeB { get; protected set; }
    public sealed void SetNodeA(INode node) => NodeA = node;
    public sealed void SetNodeB(INode node) => NodeB = node;
}

My class Segment : ISegment has the properties declared like this:

[SerializeField]
protected Node _nodeA;
public INode NodeA
{
    get => _nodeA;
    protected set => _nodeA = value as Node;
}

[SerializeField]
protected Node _nodeB;
public INode NodeB
{
    get => _nodeB;
    protected set => _nodeB = value as Node;
}

And i get this error:

'Segment' does not implement interface member 'ISegment.NodeA.set'
'Segment' does not implement interface member 'ISegment.NodeB.set'

What am i misunderstanding here?

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

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

发布评论

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

评论(1

清风疏影 2025-01-30 04:39:10

这样做吗,

    public interface ISegment
    {
        INode NodeA { get; protected set; }
        INode NodeB { get; protected set; }
        public sealed void SetNodeA(INode node) => NodeA = node;
        public sealed void SetNodeB(INode node) => NodeB = node;
    }

    public class MyClass : ISegment
    {
        private Node _nodeA;
        private Node _nodeB;

        INode ISegment.NodeA
        {
            get => _nodeA;
            set => _nodeA = value as Node;
        }

        INode ISegment.NodeB
        {
            get => _nodeB;
            set => _nodeB = value as Node;
        }

        private void Do(INode node)
        {
            if(node == NodeA) { } // Error
            if(node == _nodeA) { } // Warning "Possibly unintended reference comparison"
            if (node == ((ISegment)this).NodeA) { } // OK
        }
    }

但是问题是,你为什么想要那个?为什么受保护的集合

您也可以这样做。 (由于接口中的默认实现,在这里无济于事)


    public interface IFoo
    {
        public string Bar { get; }
    }

    public class Bubu : IFoo
    {
        public string Bar { get; protected set; }
    }

更新:编辑了代码以显示更准确的信息,如何访问属性

Do it like this

    public interface ISegment
    {
        INode NodeA { get; protected set; }
        INode NodeB { get; protected set; }
        public sealed void SetNodeA(INode node) => NodeA = node;
        public sealed void SetNodeB(INode node) => NodeB = node;
    }

    public class MyClass : ISegment
    {
        private Node _nodeA;
        private Node _nodeB;

        INode ISegment.NodeA
        {
            get => _nodeA;
            set => _nodeA = value as Node;
        }

        INode ISegment.NodeB
        {
            get => _nodeB;
            set => _nodeB = value as Node;
        }

        private void Do(INode node)
        {
            if(node == NodeA) { } // Error
            if(node == _nodeA) { } // Warning "Possibly unintended reference comparison"
            if (node == ((ISegment)this).NodeA) { } // OK
        }
    }

but the Question is, why do you want that? Why protected set?

You can do it like this also. (Not helps here because of default implementation in the interface)


    public interface IFoo
    {
        public string Bar { get; }
    }

    public class Bubu : IFoo
    {
        public string Bar { get; protected set; }
    }

Update: Edited the code to show more exactly, how to access the properties

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