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

        }
    }

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

  • 相关阅读:
    sqlserver json 查询
    分页算法
    context.Response.AddHeader("Access-Control-Allow-Origin", context.Request.Headers["Origin"]); 这个方法是有问题的,AJAX跨域解决方案 在IE11中 context.Request.Headers["Origin"] 这段是获取不到值的。
    NativeWindow 妙用,截取windows消息
    屏蔽浏览器 F12
    linux常用基础命令40条
    shell之正则
    Go语言学习思路与开发软件VScode安装
    shell基础
    docker harbor安装失败
  • 原文地址:https://www.cnblogs.com/qq4004229/p/2332165.html
Copyright © 2011-2022 走看看