简体   繁体   English

与c#中的getter和setter接口

[英]Interface with getter and setter in c#

As I read here http://msdn.microsoft.com/en-us/library/75e8y5dd%28v=VS.100%29.aspx 我在这里阅读http://msdn.microsoft.com/en-us/library/75e8y5dd%28v=VS.100%29.aspx

It is possible to have get in an Interface BUT NOT set ? 它可能有get在一个接口,但不set

OR if I want getter and setter in Interface, do I have to use the old syntax getVar setVar just because new syntax doesn't fit Interface syntax? 或者,如果我想在接口中使用getter和setter,我是否必须使用旧语法getVar setVar ,因为新语法不适合接口语法?

Update: If I must omit set in Interface, does this means I cannot enforce class to have setter which defeats the purpose of having an Interface in this case as I can only partially enforce? 更新:如果我必须省略接口中的set ,这是否意味着我不能强制类使setter在这种情况下失败了接口的目的,因为我只能部分执行?

No. I think you misunderstood. 不,我觉得你误解了。 That article is about the possibility of having an interface with a readonly property (a property with only getter). 那篇文章是关于具有readonly属性的接口的可能性 (只有getter的属性)。 But, if you need, you can put also the setter in the interface: 但是,如果需要,您也可以将setter放在界面中:

interface IHasProperty
{
    string Property{ get;set; }
}
class HasProperty:IHasProperty 
{
    public string Property{ get;set; }
}

You can use property syntax. 您可以使用属性语法。 Use this combination: 使用此组合:

interface ISomething
{
    string Test { get; }
}

class Something : ISomething
{
    public string Test { get; private set; }
}

You can of course add full implementations for the getters in Something.Test, if you choose to. 如果您选择,您当然可以在Something.Test中为getter添加完整的实现。 I only used backing fields for brevity. 为简洁起见,我只使用了支持字段。

Remember that an interface defines the bare minimum set of things you must implement. 请记住,接口定义了必须实现的最小限度的事物。 You can add any gravy (new methods, accessors, members, etc) on top that you want. 您可以根据需要添加任何肉汁(新方法,访问者,成员等)。 You could even add a public setter: 你甚至可以添加一个公共setter:

interface ISomething
{
    string Test { get; }
}

class Something : ISomething
{
    public string Test { get; set; } // Note that set is public
}

The only restriction is that someone can't use the gravy you add, unless they have a reference of the concrete type (the class, not the interface), or a different interface that defines the methods you added. 唯一的限制是有人不能使用你添加的肉汁,除非他们有具体类型(类,而不是接口)的引用,或者定义你添加的方法的不同接口。

Yes, just omit set; 是的,只是省略set; from the property declaration. 来自财产申报。 For example: 例如:

interface IName
{
    string Name { get; }
}

The answer in fact is the mixture of the above answers: omitting setter on the interface and having get; private set; 答案实际上是上述答案的混合:在界面上省略了setter并get; private set; get; private set; on the class. 在课堂上。

If you only want the get available just use {get;private set;} 如果您只想使用get,请使用{get;private set;}

http://msdn.microsoft.com/en-us/library/bb384054.aspx http://msdn.microsoft.com/en-us/library/bb384054.aspx

The class that is shown in the previous example is mutable. 上一个示例中显示的类是可变的。 Client code can change the values in objects after they are created. 客户端代码可以在创建对象后更改它们中的值。 In complex classes that contain significant behavior (methods) as well as data, it is often necessary to have public properties. 在包含重要行为(方法)和数据的复杂类中,通常需要具有公共属性。 However, for small classes or structs that just encapsulate a set of values (data) and have little or no behaviors, it is recommended to make the objects immutable by declaring the set accessor as private. 但是,对于只封装一组值(数据)并且几乎没有行为的小类或结构,建议通过将set访问器声明为private来使对象不可变。 For more information, see How to: Implement a Lightweight Class with Auto-Implemented Properties (C# Programming Guide). 有关更多信息,请参见如何:使用自动实现的属性实现轻量级类(C#编程指南)。

Attributes are permitted on auto-implemented properties but obviously not on the backing fields since those are not accessible from your source code. 允许在自动实现的属性上使用属性,但显然不在支持字段上,因为无法从源代码访问这些属性。 If you must use an attribute on the backing field of a property, just create a regular property. 如果必须在属性的支持字段上使用属性,只需创建常规属性。

You misunderstood. 你误会了。 According to the article you cannot use access modifiers on interface. 根据文章,你不能在界面上使用访问修饰符

You CAN use both get and set in interface property! 您可以在interface属性中同时使用get和set!

See in the following MSDN example: 请参阅以下MSDN示例:

http://msdn.microsoft.com/en-us/library/87d83y5b(v=VS.100).aspx http://msdn.microsoft.com/en-us/library/87d83y5b(v=VS.100).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM