interface

interface  学习

使用interface定义接口,

接口不储存数据,所以没有字段,但是可以有属性,

实现接口的类必须包括接口的所有方法和属性,否则无法编译。

公共接口中的所有方法都会自动成为公共方法,因为接口就是用来定义实现该接口的类应该具有的公共方法和属性。

不能实例化接口,不过可以引用接口。

接口也可以继承接口,如果一个接口继承了另一个接口,继承该接口的任何类必须实现另一个接口的方法和属性,

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.");
    }
}

结果为:

MethodToImplement() called.
ParentInterfaceMethod() called.

posted on 2018-03-21 22:14  Rookier2018  阅读(253)  评论(0编辑  收藏  举报