D 程式設計 - 過載



D 允許您在同一個作用域中為函式名或運算子指定多個定義,這分別稱為函式過載運算子過載

過載宣告是指在同一作用域中與之前宣告具有相同名稱的宣告,但這兩個宣告具有不同的引數,並且顯然具有不同的定義(實現)。

當您呼叫過載函式運算子時,編譯器會透過比較您用來呼叫函式或運算子的引數型別與定義中指定的引數型別來確定最合適的定義。選擇最合適的過載函式或運算子的過程稱為過載解析

函式過載

您可以在同一個作用域中為同一個函式名定義多個函式。函式的定義必須透過引數列表中型別和/或引數的數量來區分。您不能過載僅返回值型別不同的函式宣告。

示例

以下示例使用相同的函式print()來列印不同的資料型別:

import std.stdio; 
import std.string; 

class printData { 
   public: 
      void print(int i) { 
         writeln("Printing int: ",i); 
      }

      void print(double f) { 
         writeln("Printing float: ",f );
      }

      void print(string s) { 
         writeln("Printing string: ",s); 
      } 
}; 
 
void main() { 
   printData pd = new printData();  
   
   // Call print to print integer 
   pd.print(5);
   
   // Call print to print float 
   pd.print(500.263); 
   
   // Call print to print character 
   pd.print("Hello D"); 
} 

當以上程式碼編譯並執行時,會產生以下結果:

Printing int: 5 
Printing float: 500.263 
Printing string: Hello D

運算子過載

您可以重新定義或過載 D 中大多數可用的內建運算子。因此,程式設計師也可以將運算子與使用者定義的型別一起使用。

可以使用字串 op 後跟 Add、Sub 等來過載運算子,具體取決於要過載的運算子。我們可以過載運算子 + 來新增兩個盒子,如下所示。

Box opAdd(Box b) { 
   Box box = new Box(); 
   box.length = this.length + b.length; 
   box.breadth = this.breadth + b.breadth; 
   box.height = this.height + b.height; 
   return box; 
}

以下示例展示了使用成員函式進行運算子過載的概念。這裡將一個物件作為引數傳遞,其屬性使用此物件訪問。呼叫此運算子的物件可以使用this運算子訪問,如下所述:

import std.stdio;

class Box { 
   public:  
      double getVolume() { 
         return length * breadth * height; 
      }

      void setLength( double len ) { 
         length = len; 
      } 

      void setBreadth( double bre ) { 
         breadth = bre; 
      }

      void setHeight( double hei ) { 
         height = hei; 
      }

      Box opAdd(Box b) { 
         Box box = new Box(); 
         box.length = this.length + b.length; 
         box.breadth = this.breadth + b.breadth; 
         box.height = this.height + b.height; 
         return box; 
      } 

   private: 
      double length;      // Length of a box 
      double breadth;     // Breadth of a box 
      double height;      // Height of a box 
}; 

// Main function for the program 
void main( ) { 
   Box box1 = new Box();    // Declare box1 of type Box 
   Box box2 = new Box();    // Declare box2 of type Box 
   Box box3 = new Box();    // Declare box3 of type Box 
   double volume = 0.0;     // Store the volume of a box here
   
   // box 1 specification 
   box1.setLength(6.0); 
   box1.setBreadth(7.0); 
   box1.setHeight(5.0);
   
   // box 2 specification 
   box2.setLength(12.0); 
   box2.setBreadth(13.0); 
   box2.setHeight(10.0); 
   
   // volume of box 1 
   volume = box1.getVolume(); 
   writeln("Volume of Box1 : ", volume);
   
   // volume of box 2 
   volume = box2.getVolume(); 
   writeln("Volume of Box2 : ", volume); 
   
   // Add two object as follows: 
   box3 = box1 + box2; 
   
   // volume of box 3 
   volume = box3.getVolume(); 
   writeln("Volume of Box3 : ", volume);  
} 

當以上程式碼編譯並執行時,會產生以下結果:

Volume of Box1 : 210 
Volume of Box2 : 1560 
Volume of Box3 : 5400

運算子過載型別

基本上,運算子過載有三種類型,如下所示。

廣告