• 接口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);

        }
    }

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

  • 相关阅读:
    taotao-manager-service/pom.xml
    Grafana+Prometheus 监控 MySQL
    firewall-cmd 常用命令
    K8S 容器的资源需求、资源限制
    K8S 高级调度方式
    性能测试工具 Locust 安装
    cookie 和 session区别
    K8S 调度器,预选策略,优选函数
    CPU 实用工具
    *(int*)&p
  • 原文地址:https://www.cnblogs.com/qq4004229/p/2332165.html
Copyright © 2020-2023  润新知