只读,只写在接口自动属性 [英] Read only and write only automatic properties in Interface
问题描述
我已经读了自动实现属性不能只读或只写。他们只能读写。
然而,一边学习界面,我碰到FOLL。代码,它创建了一个只读/只写和读写自动属性的类型。那是可以接受的?
公共接口IPointy
{
//一个在界面看起来像读写属性:
// retType PROPNAME {搞定;组; }
//而在接口的只写属性是:
// retType PROPNAME {设置; }
字节点{搞定; }
}
这是不是自动实现。接口不包含执行。
这是一个声明,表明该接口 IPointy
需要的类型的属性字节
,名为分数
,与公共的getter 的
您可以实现以任何方式需要时只要界面是有公共的getter;无论是通过自动属性:
公共类Foo:IPointy
{
公共字节点{得到;设置;}
}
请注意二传手仍可以是私有的:
公共类酒吧:IPointy
{
公共字节点{搞定;私人集;}
}
或者,您可以明确地写一个getter:
公共类巴兹:IPointy
{
专用字节_points;
公共字节点
{
{返回_points; }
}
}
I had read that auto implemented properties cannot be read only or write only. They can only be read-write.
However, while learning interfaces I came across foll. code, which creates a read only / write only and read-write type of automatic properties. Is that acceptable?
public interface IPointy
{
// A read-write property in an interface would look like:
// retType PropName { get; set; }
// while a write-only property in an interface would be:
// retType PropName { set; }
byte Points { get; }
}
That is not auto-implemented. Interfaces do not contain implementation.
It's a declaration that the interface IPointy
requires a property of type byte
, named Points
, with a public getter.
You can implement the interface in any way necessary as long as there is a public getter; whether by an auto-property:
public class Foo: IPointy
{
public byte Points {get; set;}
}
Note the setter can still be private:
public class Bar: IPointy
{
public byte Points {get; private set;}
}
Or, you can explicitly write a getter:
public class Baz: IPointy
{
private byte _points;
public byte Points
{
get { return _points; }
}
}
这篇关于只读,只写在接口自动属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!