C# 如何在接口中实现属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1593413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement a property in an interface
提问by peter
I have interface IResourcePolicy
containing the property Version
. I have to implement this property which contain value, the code written in other pages:
我有IResourcePolicy
包含属性的接口Version
。我必须实现这个包含值的属性,在其他页面中编写的代码:
IResourcePolicy irp(instantiated interface)
irp.WrmVersion = "10.4";
How can I implement property version
?
我怎样才能实现财产version
?
public interface IResourcePolicy
{
string Version
{
get;
set;
}
}
采纳答案by Stefan Steinegger
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.
这看起来很相似,但它是完全不同的东西。在界面中,没有代码。您只需指定一个带有 getter 和 setter 的属性,无论它们会做什么。
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.
在课堂上,你实际实现了它们。执行此操作的最短方法是使用此{ get; set; }
语法。编译器将创建一个字段并为其生成 getter 和 setter 实现。
回答by Vitaliy Liptchinsky
Interfaces can not contain any implementation (including default values). You need to switch to abstract class.
接口不能包含任何实现(包括默认值)。您需要切换到抽象类。
回答by J. Random Coder
You mean like this?
你的意思是这样?
class MyResourcePolicy : IResourcePolicy {
private string version;
public string Version {
get {
return this.version;
}
set {
this.version = value;
}
}
}
回答by kazuk
- but i already assigned values such that irp.WrmVersion = "10.4";
- 但我已经分配了这样的值 irp.WrmVersion = "10.4";
J.Random Coder's answer and initialize version field.
J.Random Coder 的回答和初始化版本字段。
private string version = "10.4';
回答by Bernard Czaiński
The simple example of using a property in an interface:
在接口中使用属性的简单示例:
using System;
interface IName
{
string Name { get; set; }
}
class Employee : IName
{
public string Name { get; set; }
}
class Company : IName
{
private string _company { get; set; }
public string Name
{
get
{
return _company;
}
set
{
_company = value;
}
}
}
class Client
{
static void Main(string[] args)
{
IName e = new Employee();
e.Name = "Tim Bridges";
IName c = new Company();
c.Name = "Inforsoft";
Console.WriteLine("{0} from {1}.", e.Name, c.Name);
Console.ReadKey();
}
}
/*output:
Tim Bridges from Inforsoft.
*/
回答by Abdus Salam Azad
You should use abstract class to initialize a property. You can't inititalize in Inteface .
您应该使用抽象类来初始化属性。您不能在 Inteface 中初始化。