- D 程式設計基礎
- D 程式設計 - 首頁
- D 程式設計 - 概述
- D 程式設計 - 環境
- D 程式設計 - 基本語法
- D 程式設計 - 變數
- D 程式設計 - 資料型別
- D 程式設計 - 列舉
- D 程式設計 - 字面量
- D 程式設計 - 運算子
- D 程式設計 - 迴圈
- D 程式設計 - 條件語句
- D 程式設計 - 函式
- D 程式設計 - 字元
- D 程式設計 - 字串
- D 程式設計 - 陣列
- D 程式設計 - 關聯陣列
- D 程式設計 - 指標
- D 程式設計 - 元組
- D 程式設計 - 結構體
- D 程式設計 - 聯合體
- D 程式設計 - 範圍
- D 程式設計 - 別名
- D 程式設計 - 混入
- D 程式設計 - 模組
- D 程式設計 - 模板
- D 程式設計 - 不可變
- D 程式設計 - 檔案 I/O
- D 程式設計 - 併發
- D 程式設計 - 異常處理
- D 程式設計 - 合同
- D - 條件編譯
- D 程式設計 - 面向物件
- D 程式設計 - 類與物件
- D 程式設計 - 繼承
- D 程式設計 - 過載
- D 程式設計 - 封裝
- D 程式設計 - 介面
- D 程式設計 - 抽象類
- D 程式設計 - 有用資源
- D 程式設計 - 快速指南
- D 程式設計 - 有用資源
- D 程式設計 - 討論
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
運算子過載型別
基本上,運算子過載有三種類型,如下所示。
廣告