我有以下的interface..It有一个错误,我不知道为什么会发生。基本上,对于接口部分,string DoorDescription { get; private set; }必须删除private set以使其工作。
namespace test6
{
interface IHasExteriorDoor
{
string DoorDescription { get; private set; }
string DoorLocation { get; set; }
}
class Room : IHasExteriorDoor
{
public Room(string disc, string loc)
{
DoorDescription = disc;
DoorLocation = loc;
}
public string DoorDescription { get; private set; }
public string DoorLocation { get; set; }
}
class Program
{
static void Main(string[] args)
{
Room a = new Room("A","B");
a.DoorLocation = "alien";
//a.DoorDescription = "mars";
}
}
}发布于 2014-07-06 01:53:54
请参阅这里
接口不能包含常量、字段、运算符、实例构造函数、析构函数或类型。接口成员自动是公共的,它们不能包含任何访问修饰符。成员也不能是静态的。
基本上,一个接口是一个公共契约。
您可以将属性设置为只读,然后拥有类中实现它的私有集。。
发布于 2014-07-06 01:54:06
接口不能有任何私有方法。
只需从接口中删除setter:
interface IHasExteriorDoor
{
string DoorDescription { get; }
string DoorLocation { get; set; }
}实现它的类仍然可以有一个属性的setter,而且由于没有在接口中定义setter,所以它可以是私有的。
https://stackoverflow.com/questions/24592018
复制相似问题