在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C++/ D語言接口
類的訪問修飾符
D語言運算符
D語言邏輯運算符
類指針
D語言元組
D語言指針
D語言模塊
D語言sizeof運算符
D語言混合類型
D語言封裝
D語言條件編譯
類的靜態(tài)成員
D語言do...while循環(huán)
D語言結構體
重載
D語言字符串-String
D語言決策語句
D語言接口
D語言for循環(huán)
D語言switch語句
D語言關聯(lián)數(shù)組
D語言范圍
D語言枚舉Enums
契約式編程
D語言并發(fā)
D語言開發(fā)環(huán)境設置
D語言別名
D語言常值
D語言常量
D語言函數(shù)
D語言if嵌套語句
D語言循環(huán)
D語言概述,D語言是什么?
D語言運算符優(yōu)先級
D語言continue語句
D語言異常處理
D語言break語句
D語言if...else語句
D語言類和對象
類繼承
D語言字符
D語言教程
D語言關系運算符
比較操作符重載
構造函數(shù)和析構函數(shù)
D語言抽象類
D語言if語句
D語言賦值運算符
D中算術運算符
D語言類成員函數(shù)
D語言位運算符
D語言變量
D語言數(shù)據(jù)類型
D語言文件I/O
D語言數(shù)組
一元運算符重載
D語言嵌套switch語句
D語言基本語法
二元運算符重載
this指針
D語言聯(lián)合體
D語言模板
D語言嵌套循環(huán)
D語言while循環(huán)

D語言接口

接口是迫使從它繼承的類必須實現(xiàn)某些功能或變量的方法。函數(shù)不能在一個接口來實現(xiàn),因為它們在從接口繼承的類總是執(zhí)行。使用interface關鍵字代替,盡管兩者在很多方面是相似的class關鍵字創(chuàng)建一個接口。當你想從一個接口繼承和類已經(jīng)從另一個類繼承,那么需要單獨的類的名稱,并用逗號將接口的名稱。

讓我們來看一個簡單的例子,說明使用的接口。

import std.stdio;

// Base class
interface Shape
{
   public:
      void setWidth(int w);
      void setHeight(int h);
}

// Derived class
class Rectangle: Shape
{
   int width;
   int height;
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }

      int getArea()
      {
         return (width * height);
      }
}

void main()
{
   Rectangle Rect = new Rectangle();

   Rect.setWidth(5);
   Rect.setHeight(7);

   // Print the area of the object.
   writeln("Total area: ", Rect.getArea());

}

當上面的代碼被編譯并執(zhí)行,它會產(chǎn)生以下結果:

Total area: 35

Final和靜態(tài)函數(shù)接口

一個接口可以有最終的和靜態(tài)方法的定義應包括在接口本身。這些功能不能過度由派生類重載。一個簡單的例子如下所示。

import std.stdio;

// Base class
interface Shape
{
   public:
      void setWidth(int w);
      void setHeight(int h);
      static void myfunction1()
      {
         writeln("This is a static method");
      }
      final void myfunction2()
      {
         writeln("This is a final method");
      }
}

// Derived class
class Rectangle: Shape
{
   int width;
   int height;
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }

      int getArea()
      {
         return (width * height);
      }
}

void main()
{
   Rectangle rect = new Rectangle();

   rect.setWidth(5);
   rect.setHeight(7);

   // Print the area of the object.
   writeln("Total area: ", rect.getArea());
   rect.myfunction1();
   rect.myfunction2();
}

讓我們編譯和運行上面的程序,這將產(chǎn)生以下結果:

Total area: 35
This is a static method
This is a final method

上一篇:D語言賦值運算符下一篇:類指針