如何实现一个接口的属性 [英] How to implement a property in an interface
问题描述
我有一个包含属性界面 IResourcePolicy
版本
。我要实现这个属性包含值,写在其他页面的代码:
IResourcePolicy IRP(实例化接口)
irp.WrmVersion =10.4;
我如何能实现财产版本
?
公共接口IResourcePolicy
{
字符串版本
{
获得;
组;
}
}
在在接口方面,您指定的属性:
公共接口IResourcePolicy
{
字符串版本{搞定;组; }
}
在实现类,需要实现它:
公共类ResourcePolicy:IResourcePolicy
{
公共字符串版本{搞定;组; }
}
这看起来相似,但它是完全不同的东西。在接口,没有代码。您只需指定,有一个getter和一个setter,不管他们会做一个属性。
在类中,你实际上执行。要做到这一点,最简单的办法就是采用这种 {搞定;组; }
语法。编译器将创建一个字段,并为它生成getter和setter的实现。
I have interface IResourcePolicy
containing the property Version
. I have to implement this property which contain value, the code written in other pages:
IResourcePolicy irp(instantiated interface)
irp.WrmVersion = "10.4";
How can I implement property version
?
public interface IResourcePolicy
{
string Version
{
get;
set;
}
}
In the interface, you specify the property:
public interface IResourcePolicy
{
string Version { get; set; }
}
In the implementing class, you need to implement it:
public class ResourcePolicy : IResourcePolicy
{
public string Version { get; set; }
}
This looks similar, but it is something completely different. In the interface, there is no code. You just specify that there is a property with a getter and a setter, whatever they will do.
In the class, you actually implement them. The shortest way to do this is using this { get; set; }
syntax. The compiler will create a field and generate the getter and setter implementation for it.
这篇关于如何实现一个接口的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!