国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

java中,this用途總結(jié)。。。

2019-11-18 14:28:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  注:參考了JIT(java in thinking),示例都是那里面的。
1、在一個(gè)類(lèi)中,this可以表示該類(lèi)的當(dāng)前實(shí)例;例如:
public class Leaf {
PRivate int i = 0;
Leaf increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();
}
} ///:~
2、若為一個(gè)類(lèi)寫(xiě)了多個(gè)構(gòu)造器,那么經(jīng)常都需要在一個(gè)構(gòu)造器里調(diào)用另一個(gè)構(gòu)造器,以避免寫(xiě)重復(fù)的代碼。這時(shí)可以使用this,例如:
//: Flower.java
// Calling constrUCtors with "this"

public class Flower {
private int petalCount = 0;
private String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println(
"Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only, s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
//! this(11); // Not inside non-constructor!
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
} ///:~

而需要注重的是:在一個(gè)類(lèi)A的內(nèi)部類(lèi)B中使用this它表示的并非是A.B的當(dāng)前實(shí)例,而是A的當(dāng)前實(shí)例;

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 全南县| 晋州市| 乳源| 来宾市| 太原市| 舒兰市| 南平市| 刚察县| 利津县| 竹溪县| 定南县| 修水县| 靖安县| 翁牛特旗| 太原市| 越西县| 垫江县| 辽宁省| 紫阳县| 织金县| 隆回县| 竹北市| 大洼县| 如东县| 花垣县| 临潭县| 循化| 宁南县| 庆城县| 和静县| 喀什市| 永胜县| 布拖县| 平果县| 太康县| 察隅县| 紫阳县| 雅江县| 灵台县| 英超| 五寨县|