林下微风 發表於 2020-8-28 23:45:00

C#-接口(Interface)详解

<h2>定义</h2>
<p><span style="font-size: 15px">在 C# 语言中,类之间的继承关系仅支持单重继承,而接口是为了实现多重继承关系设计的。一个类能同时实现多个接口,还能在实现接口的同时再继承其他类,并且接口之间也可以继承。无论是表示类之间的继承还是类实现接口、接口之间的继承,都使用“:”来表示。</span></p>
<p><span style="font-size: 15px">接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分。</span></p>
<h4>定义接口语法:</h4>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">interface接口名称
{
    接口成员;
}</pre>
</div>
<p><span style="font-size: 15px">接口命名通常以 I 字母开头,例如Itest。</span><br><span style="font-size: 15px">接口成员,不允许使用 public、private、protected、internal 访问修饰符,不允许使用 static、virtual、abstract、sealed 修饰符。不能定义字段,定义的方法不能包含方法体。</span></p>
<p><span style="font-size: 15px">示例:定义一本书的接口,有id、名称name、定价price几个属性,和一个方法售卖价SalePrice()。</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">using System;

namespace app
{
    interface IBook
    {
      int Id { get; set; }
      string Name { get; set; }
      double Price { get; set; }
      double SalePrice(int discount);
    }
}  </pre>
</div>
<h2>实现</h2>
<p><span style="font-size: 15px">接口的实现与类的继承语法格式类似,也是重写了接口中的方法,让其有了具体的实现内容。</span></p>
<h3><span style="font-size: 15px">实现接口语法:</span></h3>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">class类名 : 接口名
{
    //类中的成员以及实现接口中的成员
}</pre>
</div>
<p><span style="font-size: 15px">在实现接口的成员时有两种方式,隐式实现接口成员和显式实现接口成员。隐式实现接口成员是将接口的所有成员以 public 访问修饰符修饰。显式实现接口是指在实现接口时所实现的成员名称前含有接口名称作为前缀。</span><br><span style="font-size: 15px">下面对一本书的接口分别使用隐式实现和显式实现。</span></p>
<h3>隐式实现:</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">   class Book:IBook
    {
      // 隐式的实现接口中的属性
      public int Id { get; set; }
      public string Name { get; set; }
      public double Price { get; set; }

      // 隐式实现接口中的方法
      public double SalePrice(int discount)
      {
            double salePrice = Price * discount * 0.1;
                        return salePrice;
      }

    } </pre>
</div>
<p><span style="font-size: 15px">隐式实现后进行运行:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">    class Program
    {
      static void Main(string[] args)
      {
            Book book = new Book();
            book.Id = 1001;
            book.Name = "tynam";
            book.Price = 60;

            Console.WriteLine("id:{0}" , book.Id);
            Console.WriteLine("书名:{0}" , book.Name);
            Console.WriteLine("定价:{0}", book.Price);
            Console.WriteLine("售卖价:{0}", book.SalePrice(8));
      }
    }
</pre>
</div>
<p>  </p>
<p><span style="font-size: 15px">运行后结果</span>:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">id:1001
书名:tynam
定价:60
售卖价:48</pre>
</div>
<h3>显式实现:<br>    </h3>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">    class Book:IBook
    {
      public double Price { get; set; }
      // 显示的实现接口中的属性
      int IBook.Id { get; set; }
      string IBook.Name { get; set; }

      // 显式实现接口中的方法
      double IBook.SalePrice(int discount)
      {
            double salePrice = Price * discount * 0.1;
                        return salePrice;
      }

    }</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 15px">显式实现后进行运行:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">    class Program
    {
      static void Main(string[] args)
      {
            Book book = new Book();
            IBook bookDetail = book;
                        bookDetail.Id = 1001;
            bookDetail.Name = "tynam";
            bookDetail.Price = 60;

            Console.WriteLine("id:{0}" , bookDetail.Id);
            Console.WriteLine("书名:{0}" , bookDetail.Name);
            Console.WriteLine("定价:{0}", bookDetail.Price);
            Console.WriteLine("售卖价:{0}", bookDetail.SalePrice(8));
      }
    }</pre>
</div>
<p><span style="font-size: 15px">运行后结果:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">id:1001
书名:tynam
定价:60
售卖价:48</pre>
</div>
<p><span style="font-size: 15px">注意:接口无法直接进行实例化。 其成员由实现接口的任何类或结构来实现。</span></p>
<h2>多态</h2>
<p><span style="font-size: 15px">使用接口实现多态 需要满足以下两个条件。</span><br><span style="font-size: 15px">定义接口并使用类实现了接口中的成员。</span><br><span style="font-size: 15px">创建接口的实例指向不同的实现类对象。</span></p>
<p><span style="font-size: 15px">示例:定义一个接口名称为 IBird,分别定义两个实现类 Phoenix 和 Duck 来实现接口的成员,代码如下。</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">    interface IBird
      {
            void fly();
      }

      class Phoenix : IBird
      {
            public void fly()
            {
                Console.WriteLine("凤凰会飞");
            }
      }

      class Duck : IBird
      {
            public void fly()
            {
                Console.WriteLine("鸭子也会飞");
            }      
      }</pre>
</div>
<p><span style="font-size: 15px">实例化后执行:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">      class Program
      {
                static void Main(string[] args)
                {
                        IBird phoenix = new Phoenix();
                        phoenix.fly();
                        IBird duck = new Duck();
                        duck.fly();
                }
      }</pre>
</div>
<p><span style="font-size: 15px">执行结果:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">凤凰会飞
鸭子也会飞
</pre>
</div>
<p>  </p><br><br>
来源:https://www.cnblogs.com/tynam/p/13580361.html
頁: [1]
查看完整版本: C#-接口(Interface)详解