W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
D語言中的每個對象都可以通 this 訪問自己的指針地址, this 指針是所有成員函數(shù)的隱式參數(shù)。
讓我們嘗試以下示例以了解 this 指針的概念-
import std.stdio;
class Box {
public:
//Constructor definition
this(double l=2.0, double b=2.0, double h=2.0) {
writeln("Constructor called.");
length=l;
breadth=b;
height=h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
return this.Volume() > box.Volume();
}
private:
double length; //Length of a box
double breadth; //Breadth of a box
double height; //Height of a box
}
void main() {
Box Box1=new Box(3.3, 1.2, 1.5); //Declare box1
Box Box2=new Box(8.5, 6.0, 2.0); //Declare box2
if(Box1.compare(Box2)) {
writeln("Box2 is smaller than Box1");
} else {
writeln("Box2 is equal to or larger than Box1");
}
}
編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: