简体   繁体   English

如何在接口中实现属性

[英]How to implement a property in an interface

I have interface IResourcePolicy containing the property Version . 我有包含属性Version接口IResourcePolicy 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 ? 我该如何实现属性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. 您只需指定存在具有getter和setter的属性,无论它们将执行什么操作。

In the class, you actually implement them. 在课堂上,你实际上实现了它们。 The shortest way to do this is using this { get; set; } 最简单的方法是使用这个{ get; set; } { get; set; } { get; set; } syntax. { get; set; }语法。 The compiler will create a field and generate the getter and setter implementation for it. 编译器将创建一个字段并为其生成getter和setter实现。

You mean like this? 你的意思是这样的?

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. 您需要切换到抽象类。

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.
 */
  • 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';

You should use abstract class to initialize a property. 您应该使用抽象类来初始化属性。 You can't inititalize in Inteface . 你不能在Inteface中初始化。

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

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