无法使用接口中的受保护集实现 属性
Cannot implement property with protected set from interface
我有一个接口,它有一个 属性 和 public get 和 protected 集。
然而,当我在我的 class 中实现它时,我收到一条错误消息,指出它必须是 public。
我的界面是这样的:
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;
}
我的 class Segment : 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'
我在这里误解了什么?
这样做
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
}
}
但问题是,您为什么要这样做?为什么 protected set
?
你也可以这样做。 (由于接口中的默认实现,这里没有帮助)
public interface IFoo
{
public string Bar { get; }
}
public class Bubu : IFoo
{
public string Bar { get; protected set; }
}
更新: 编辑代码以更准确地显示如何访问属性
我有一个接口,它有一个 属性 和 public get 和 protected 集。
然而,当我在我的 class 中实现它时,我收到一条错误消息,指出它必须是 public。
我的界面是这样的:
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;
}
我的 class Segment : 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'
我在这里误解了什么?
这样做
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
}
}
但问题是,您为什么要这样做?为什么 protected set
?
你也可以这样做。 (由于接口中的默认实现,这里没有帮助)
public interface IFoo
{
public string Bar { get; }
}
public class Bubu : IFoo
{
public string Bar { get; protected set; }
}
更新: 编辑代码以更准确地显示如何访问属性