目录

D Programming - 重载

D允许您为同一范围内的function名称或operator指定多个定义,分别称为function overloadingoperator overloading

重载声明是一个声明,它声明的名称与同一作用域中的前一个声明的名称相同,除了两个声明都有不同的参数和明显不同的定义(实现)。

当您调用重载functionoperator ,编译器通过将用于调用函数或运算符的参数类型与定义中指定的参数类型进行比较来确定要使用的最合适的定义。 选择最合适的重载函数或运算符的过程称为overload resolution.

功能重载

您可以在同一范围内对同一函数名称具有多个定义。 函数的定义必须通过参数列表中的参数的类型和/或数量彼此不同。 您不能重载仅由返回类型不同的函数声明。

例子 (Example)

以下示例使用相同的函数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

运算符重载类型

基本上,下面列出了三种类型的运算符重载。

Sr.No. 重载类型
1 一元运算符重载
2 二进制运算符重载
3 比较运算符重载
↑回到顶部↑
WIKI教程 @2018