我有IResourcePolicy
包含该属性的接口Version
.我必须实现这个包含值的属性,代码写在其他页面中:
IResourcePolicy irp(instantiated interface)
irp.WrmVersion = "10.4";
Run Code Online (Sandbox Code Playgroud)
我该如何实施财产version
?
public interface IResourcePolicy
{
string Version
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*ger 275
在界面中,指定属性:
public interface IResourcePolicy
{
string Version { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在实现类中,您需要实现它:
public class ResourcePolicy : IResourcePolicy
{
public string Version { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这看起来很相似,但它完全不同.在界面中,没有代码.您只需指定存在具有getter和setter的属性,无论它们将执行什么操作.
在课堂上,你实际上实现了它们.最简单的方法是使用此{ get; set; }
语法.编译器将创建一个字段并为其生成getter和setter实现.
J. *_*der 21
你的意思是这样的?
class MyResourcePolicy : IResourcePolicy {
private string version;
public string Version {
get {
return this.version;
}
set {
this.version = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Vit*_*sky 15
接口不能包含任何实现(包括默认值).您需要切换到抽象类.
小智 7
在接口中使用属性的简单示例:
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.
*/
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
158666 次 |
最近记录: |