C# 接口

一 接口

接口(interface)实际上是一个约定。
如:ICloneable,IComparable;
接口是抽象成员的集合;
ICIonable含有方法clone();
IComparable含有方法compare();
接口是一个引用类型,比抽象类更抽象。

帮助实现多重继承
在这里插入图片描述

二 接口的用处

1 实现不相关的相同行为

不需要考虑这些类之间的层次关系;

2 通过接口可以了解对象的交互界面,而不需了解对象所对应的类

例如:

public sealed class  String:IComparable,ICloneable,
IConvertible,IEnumerable

三 定义有一个接口

public interface IStringList
{
    void Add(string s);
    int Count{get;}
    string this[int index]{get;set}
}

注:
public abstract 这两个关键词不写出来。

1 实现接口

class 类名:[父类,]接口,接口,....,接口
{
   public 方法(){.....}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
    interface Runner
    {
        void run();
    }

    interface Swimmer
    {
        void swim();
    }

    abstract class Animal
    {
        abstract public void eat();
    }

    class Person:Animal,Runner,Swimmer
    {
        public void run()
        {
            Console.WriteLine("run");
        }

        public void swim()
        {
            Console.WriteLine("swim");
        }

        public override void eat()
        {
            Console.WriteLine("eat");
        }

        public void speak()
        {
            Console.WriteLine("speak");
        }
    }

    class TestInterface
    {
        static void m1(Runner r)
        {
            r.run();
        }
        static void m2(Swimmer s)
        {
            s.swim();
        }

        static void m3(Animal a)
        {
            a.eat();
        }

        static void m4(Person p)
        {
            p.speak();
        }

        public static void Main(string[] args)
        {
            Person p = new Person();
            m1(p);
            m2(p);
            m3(p);
            m4(p);
            Runner a = new Person();
            a.run();
            Console.ReadKey();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 显示成员接口实现
{
   class InterfaceExplicitImpl
    {
        static void Main()
        {
            FileViewer f = new FileViewer();
            f.Test();
            ((IWindow)f).Close();

            IWindow w = new FileViewer();
            w.Close();
            Console.ReadKey();
        }
    }

    interface IWindow
    {
        void Close();
    }
    interface IFileHandler
    { 
        void Close(); 
    }
    
    class FileViewer:IWindow,IFileHandler
    {
        void IWindow.Close()
        {
            Console.WriteLine("Window Closed");
        }

        void IFileHandler.Close()
        {
            Console.WriteLine("File Closed");
        }

        public void Test()
        {
           ( (IWindow)this).Close();
        }
    }
}

总结:
接口是一种约定;
接口中有多个抽象方法;
接口能实现多继承;
面向接口进行编程;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值