zoukankan      html  css  js  c++  java
  • C# 类(15) 接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
    /*接口只声明、无实现、不能实例化;
    接口可包含方法、属性、事件、索引器, 但无字段;
    接口成员都是隐式的 public, 不要使用访问修饰符;
    
    类、结构和接口都可以继承多个接口;
    继承接口的类必须实现接口成员, 除非是抽象类;
    类实现的接口成员须是公共的、非静态的.*/
    
       // 定义接口
        interface Myinterface
        { 
            //在接口里面声明一个方法, 没有实现. 也不能有实现
            int Num(int i, int s);
        }
    
        // 实现接口
        class MyClass : Myinterface
        {
           public int Num(int i, int s) //由于接口里面的成员都是public 所以实现的时候也要是public , 否则会报错.
            { return i + s; }
        }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
        //下面试试一个接口多个实现.
    
        interface Myinterface1
        {
            int Num(int i, int s);
        }
        // 多个实现.
        class Add : Myinterface1  // 实现1 加法
        {
            public int Num(int i, int s)
            { return i + s; }
        }
    
        // 实现2  减法
        class Sub : Myinterface1
        {
            public int Num(int i, int s)
            { return i - s; }
        }
        
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        // 多个接口一个类实现.
        interface face
        { int Num(int i, int s);}
        interface face1
        { int Num(int i, int s);}
        // 一个类来实现两个接口,并且显示的实现.
        /* 显示实现接口不需要访问修饰符; 但显示实现的方法只能通过接口访问 */
        class MyFace : face, face1
        { int face.Num(int i, int s) { return i + s; } // 实现第一个. 
          int face1.Num(int i, int s) { return i - s; } // 实现第二个.
        }
        
    
        class Program
        {
            static void Main(string[] args)
            {
                //单个接口单个实现
                MyClass My = new MyClass();
                Console.WriteLine(My.Num(10, 9)); // 19
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //一个接口的多个实现.
                Add jiafa = new Add();
                Console.WriteLine(jiafa.Num(56,35)); //91
                Sub jianfa = new Sub();
                Console.WriteLine(jianfa.Num(56,35));//21
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                // 多个接口一个类实现. 只能通过接口访问.
                face  Myjiafa = new MyFace();
                face1 Myjianfa = new MyFace();
                Console.WriteLine(Myjiafa.Num(20,10));//30
                Console.WriteLine(Myjianfa.Num(20,10));//10
                
            }
        }
    }
  • 相关阅读:
    坐标转换convertRect
    error this is not a media message!!!
    嵌入式-第一季-第4课
    嵌入式-第一季-第2课
    嵌入式-第一季-第3课
    嵌入式-第一季-第1课
    web-15. 事件与函数
    web-14. 表达式与程序流程
    web-13. 数组和字符串
    数据-第5课-线性表的本质
  • 原文地址:https://www.cnblogs.com/mdnx/p/2714454.html
Copyright © 2011-2022 走看看