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

     

    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);

        }
    }

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

  • 相关阅读:
    函数
    字符串格式化
    集合
    习题02
    int/str/list/tuple/dict必会
    元组/字典
    列表方法
    练习题(format、expandtabs、片层)
    字符串方法
    JMM
  • 原文地址:https://www.cnblogs.com/DemoLee/p/2329849.html
Copyright © 2011-2022 走看看