How to implement a property in an interface
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; }
{ get; set; }
{ get; set; }
syntax. The compiler will create a field and generate the getter and setter implementation for it.
你的意思是这样吗?
class MyResourcePolicy : IResourcePolicy {
private string version;
public string Version {
get {
return this.version;
}
set {
this.version = value;
}
}
}
Interfaces can not contain any implementation (including default values). You need to switch to abstract class.
链接地址: http://www.djcxy.com/p/38144.html上一篇: 继承和接口
下一篇: 如何在界面中实现一个属性