简体   繁体   English

接口中具有不同访问器的自动属性

[英]Auto properties with different accessors in an Interface

is there any diffrence between this 3 auto properties ? 这3个自动属性之间有什么区别吗?

interface MyInterface {
    public int p1 { get; set; }
    public int p2 { get; }
    public int p3 { set; }
}

also why we can write this code in an interface but not in a class ? 还有为什么我们可以在接口中而不是在类中编写此代码?

public int p { get; }

For the same reason you can write this in an interface: 出于相同的原因,您可以在接口中编写此代码:

interface IFace {
    void Test();
}

Also, your interface is invalid, as public isn't valid in an interface. 另外,您的界面无效,因为public在界面中无效。 The point being, different things are legal in interfaces and classes. 关键是,接口和类中的不同事物是合法的。

When you do public int P1 { get; set; } 当您执行public int P1 { get; set; } public int P1 { get; set; } public int P1 { get; set; } in a class, that turns into a auto property. public int P1 { get; set; }转换成一个auto属性。 However, you can't do public int P1 { get; } 但是,您不能执行public int P1 { get; } public int P1 { get; } , because what would you want that to mean? public int P1 { get; } ,因为您希望这意味着什么? Should it always return 0? 它应该总是返回0吗? There is no way to set it. 无法进行设置。 So if you want a read only property you have to define the getter yourself like this: 因此,如果要使用只读属性,则必须自己定义getter,如下所示:

int _p1;

public int P1 {
    get { return _p1; }
}

Also. 也。 Another way to achieve more or less the same is this: 实现或多或少相同的另一种方法是:

public int P1 { get; private set; }

There are differences between those properties. 这些属性之间存在差异。 Firstly, you should remove the public modifier from your declaration. 首先,您应该从声明中删除public修饰符。 Secondly, by putting get or set within the block you define what properties in derived classes should look like. 其次,通过将getset放置在块中,您可以定义派生类中的什么样的属性。 For example, public int p1 { get; set; } 例如, public int p1 { get; set; } public int p1 { get; set; } public int p1 { get; set; } requires getter and setter in a derived class, public int p2 { get; } public int p1 { get; set; }在派生类public int p2 { get; } public int p2 { get; } only getter, and public int p3 { set; } public int p2 { get; }仅getter,而public int p3 { set; } public int p3 { set; } requires only setter to be implemented. public int p3 { set; }仅需要执行setter。

You can't use access modifiers inside interfaces because interfaces are guidelines for other developers that force them to go in a certain direction when developing the implementing classes. 您不能在接口内部使用访问修饰符,因为接口是其他开发人员的指南,这些指南会在开发实现类时迫使它们朝某个方向发展。

Look at this post for more information about that. 有关更多信息,请参见这篇文章。

Keep in mind interface does NOT contain any implementation data. 注意界面不包含任何实施数据。 When you add property in an interface, it merely says that a class implementing this interface needs to have said property with get, set or both methods, depending on what you wrote. 在接口中添加属性时,它仅表示实现此接口的类需要具有具有get,set或这两种方法的属性,具体取决于您编写的内容。 So any class implementing your interface has to implement (or have auto-generated) p1 property with get and set method, p2 with get method, and p3 with set method. 因此,任何实现您接口的类都必须使用get和set方法来实现(或具有自动生成的)p1属性,使用get方法的p2和使用set方法的p3。 Interface doesn't care whether these will be auto-generated or your own custom implementations, they just have to be in an implementing class. Interface不在乎这些是自动生成的还是您自己的自定义实现,它们只需要在实现类中即可。

Therefore, you can write 因此,您可以编写

int p { get; }

in an interface as all it does is telling that any class implementing this interface has to have property p with getter, again, not caring about its actual implementation - you could write a getter that does some computations, returns some constant, etc. OTOH in a class writing the same would mean that you want a property with auto-generated backing field, except since it would have no setter, you couldn't actually change its value, so it would always have its default value 0. 在接口中,它所做的一切都表明,实现此接口的任何类都必须具有带有getter的属性p,而不是在乎其实际实现-您可以编写一个执行一些计算,返回一些常量等操作的getter。编写相同内容的类意味着您想要一个具有自动生成的后备字段的属性,但由于它没有设置器,因此您实际上无法更改其值,因此它将始终具有默认值0。

And as noted, you cannot write access modifiers in an interface, as all interface members are implicitly public. 并且如前所述,您不能在接口中编写访问修饰符,因为所有接口成员都是隐式公共的。

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

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