zoukankan      html  css  js  c++  java
  • 接口Interface

    1:一个接口可以声明多个或者零个成员

    2:接口的成员必须是方法、属性、事件或者索引器

    3:接口不能包含常量、字段、运算符、实力构造函数、析构函数或者类型,也不能包含任何种类的静态成员

    4:所有的接口成员都隐式的具有public访问属性

    5:接口成员声明中包含任何修饰符都属于编译时错误,具体来说不能使用修饰符:abstruct、public、protected、internal、private、virtual、overried、或者static来声明接口成员

    using System;

    interface IDrivingLicenceB
    {
        void getLicence();
    }
    interface IDrivingLicenceA : IDrivingLicenceB
    {
        new void getLicence();//注意new
    }
    class Teacher:IDrivingLicenceA
    {
        public void getLicence()
        {
            Console.WriteLine("老师获得A驾驶执照");
        }
    }
    class Student : IDrivingLicenceB
    {
        public void getLicence()
        {
            Console.WriteLine("学生获得B驾驶执照");
        }
    }
    class Text
    {
        static void DriveCar(string name, IDrivingLicenceB o)
        {
            IDrivingLicenceB dl = o as IDrivingLicenceB;
            if (dl != null)
            {
                Console.WriteLine(name + "可以驾驶汽车");
            }
            else
            {
                Console.WriteLine(name+"没有驾照不能开车");
            }
        }
        static void DriveBus(string name, IDrivingLicenceB o)
        {
            IDrivingLicenceA dl = o as IDrivingLicenceA;
            if (dl != null)
            {
                Console.WriteLine(name + "可以驾驶汽车");
            }
            else
            {
                Console.WriteLine(name + "没有驾照不能开车");
            }
        }
        static void Main()
        {
            Teacher t = new Teacher();
            DriveCar("老师",t);
            DriveBus("老师", t);
            Student s = new Student();
            DriveCar("学生", s);
            DriveBus("学生", s);

        }
    }

    显示接口成员实现---可以消除因同时含有多个相同签名的接口成员所引起的多义性

  • 相关阅读:
    C. Karen and Game
    BZOJ2134: 单选错位
    BZOJ3562: [SHOI2014]神奇化合物
    BZOJ1084: [SCOI2005]最大子矩阵
    BZOJ5039: [Jsoi2014]序列维护
    BZOJ1798: [Ahoi2009]Seq 维护序列seq
    BZOJ3932: [CQOI2015]任务查询系统
    BZOJ3339: Rmq Problem
    BZOJ3585: mex
    BZOJ4196: [Noi2015]软件包管理器
  • 原文地址:https://www.cnblogs.com/qq4004229/p/2332165.html
Copyright © 2011-2022 走看看