首页 > Windows开发 > 详细

20-C#笔记-接口

时间:2018-05-21 22:21:48      阅读:241      评论:0      收藏:0      [点我收藏+]

# 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

 

20-C#笔记-接口

原文:https://www.cnblogs.com/alexYuin/p/9069538.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!