How to implement a property in an interface

后端 未结 6 1151
醉话见心
醉话见心 2020-12-04 11:43

I have interface IResourcePolicy containing the property Version. I have to implement this property which contain value, the code written in other

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 12:21

    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.
     */
    

提交回复
热议问题