20-C#笔记-接口

# 1 接口的使用示例

使用interface,关键字

接口的实现和使用,和继承类似。

在使用之前,要实现接口。

 

using System;

interface IMyInterface
{
    // 接口成员
    void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.MethodToImplement();
    }

    public void MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }
}

  

# 2 接口的继承

在继承接口的类中,要实现所有的接口

using System;

interface IParentInterface
{
    void ParentInterfaceMethod();
}

interface IMyInterface : IParentInterface
{
    void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.MethodToImplement();
        iImp.ParentInterfaceMethod();
    }

    public void MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }

    public void ParentInterfaceMethod()
    {
        Console.WriteLine("ParentInterfaceMethod() called.");
    }
}

  

参考:

http://www.runoob.com/csharp/csharp-interface.html

 

posted @ 2018-05-21 21:50  路边的十元钱硬币  阅读(168)  评论(0编辑  收藏  举报