zoukankan      html  css  js  c++  java
  • C#之接口

    一 接口

         接口只是定义了可由类或结构实现的协定,其可包括方法,属性,事件,索引器,但不提供方法的实现,仅指定必须由实现接口的类或者结构提供。

        代码演示:

        public interface IDataBound
        {
            void Bind(string str);
        }
        public interface IControl
        {
            void paint();
        }
        public class EditBox: IControl, IDataBound
        {
           //下面两个同名接口区别
            public void paint()
            {
                Console.WriteLine("EditBox Interface IControl");
             }
            void IControl.paint()
            {
                Console.WriteLine("Interface IControl");
            }
            public void Bind(string str)
            {
                Console.WriteLine("EditBox Interface IDataBound");
             }
        }
     static void Main(string[] args)
            {
                //当类或结构实现特定接口时,此类或结构的实例可以隐式转换成相应的接口类型
                EditBox editBox = new EditBox();
                IControl iControl = editBox;
                editBox.paint();
                //在类中显示实现接口成员时,当接口变量调用成员时,会强制使用接口中的成员方法(带有句点符号的那个方法)。
                iControl.paint();
                //下面的调用仍然是显示实现接口成员。
                object obj = new EditBox();
                IControl control = (IControl)obj;
                control.paint();
                Console.ReadKey();
            }
        }
     
           输出结果:
             EditBox Interface IControl
             Interface IControl
             Interface IControl
         实现接口的类可以显式实现该接口的成员。 显式实现的成员不能通过类实例访问,而只能通过接口实例访问。

    二 接口属性

       所谓接口属性就是在接口定义一个属性成员:

       public interface ISampleInterface
       {
           // Property declaration:
            string Name
           {
              get;
              set;
           }
        }

       接口属性的访问器没有正文因此,访问器的用途是指示属性为读写、只读还是只写。

    interface IEmployee
    {
        string Name     //读写
        {
            get;
            set;
        }

        int Counter    //只读
        {
            get;
        }
     }

    public class Employee : IEmployee
    {
        public static int numberOfEmployees;    //statc修饰变量只属于类,各个对象共享,因此调用该类型的变量时,使用的是类名Employee.numberOfEmployees

        private string name;
        public string Name  // read-write instance property
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        private int counter;
        public int Counter  // read-only instance property
        {
            get
            {
                return counter;
            }
        }

        public Employee(// constructor
        {
            counter = ++numberOfEmployees;
        }
    }

    class TestEmployee
    {
        static void Main()
        {
            System.Console.Write("Enter number of employees: ");
            Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());   //将输入的数据中包含的字符串转换成int

            Employee e1 = new Employee();
            System.Console.Write("Enter the name of the new employee: ");
            e1.Name = System.Console.ReadLine();

            System.Console.WriteLine("The employee information:");
            System.Console.WriteLine("Employee number: {0}", e1.Counter);
            System.Console.WriteLine("Employee name: {0}", e1.Name);
        }
    }

  • 相关阅读:
    【[国家集训队]最长双回文串】
    【[国家集训队]Crash的数字表格 / JZPTAB】
    【YY的GCD】
    【于神之怒加强版】
    【[SDOI2015]约数个数和】
    【[TJOI2010]电影迷】
    【[SDOI2011]拦截导弹】
    【[SDOI2014]数表】
    linux系统编程之管道(三)
    linux系统编程之管道(二)
  • 原文地址:https://www.cnblogs.com/xingyuanzier/p/10271724.html
Copyright © 2011-2022 走看看