關於 this 關鍵字的用法
看了thinking in Java 的第五章,才發現其中對初學者的不友好之處,在this關鍵字這節,其採用的代碼難以理解,很複雜,卻又沒有好好的解釋清楚,故寫篇心得表達看法。
1. Peel方法的調用
//: initialization/BananaPeel.java
package initialization; /* Added by Eclipse.py */
class Banana { void peel(int i) { /* ... */ } }
public class BananaPeel {
public static void main(String[] args) {
Banana a = new Banana(),
b = new Banana();
a.peel(1);
b.peel(2);
}
} ///:~
首先本書就沒有介紹創建一個新的對象可以中間加“逗號,”隔開來創建
壓根就沒有解釋清楚所謂的“被a,被b調用”的問題
2. 需要返回對當前對象的引用
public class Leaf {
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();
}
需要明確指出對當前對象的引用,使用this關鍵字。
即本例:需要返回對當前對象的引用。
3. 需要將當前對象傳遞給其他方法(代碼特別難理解)
package initialization; /* Added by Eclipse.py */
class Person {
public void eat(Apple apple) {
Apple peeled = apple.getPeeled();
System.out.println("Yummy");
}
}
class Peeler {
static Apple peel(Apple apple) {
// ... remove peel
return apple; // Peeled
}
}
class Apple {
Apple getPeeled() { return Peeler.peel(this); }
}
public class PassingThis {
public static void main(String[] args) {
new Person().eat(new Apple());
}
}
講真,這段代碼真的看了好久。
不明白的地方有着幾處,
①爲什麼需要調用Peeler.peel()方法,卻沒有new一個對象
②將當前對象傳遞給其他方法,當前對象指的是哪個?
③eat函數中(Apple apple)分別代表什麼 ?
解決:Apple apple 表示傳遞對象的名稱是apple,類型是Apple
這些都是很困擾的問題,等待以後懂了以後解決。
整段代碼理解:先new Person()新建一個無對象名的對象,.eat(new Apple())表明調用eat(Apple apple)函數,其中新建一個Apple類的對象。返回類型爲Apple,調用了Peel方法{還是不懂爲什麼沒有創建Peeler對象(解決:因爲peeler類是static類,可以通過類本身來調用static方法)}返回值也是apple,類型爲Apple。
看懂的點:通過this在構造器中調用構造器
import static net.mindview.util.Print.*;
public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals) {
petalCount = petals;
print("Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
print("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"
print("String & int args");
}
Flower() {
this("hi", 47);
print("default constructor (no args)");
}
void printPetalCount() {
//! this(11); // Not inside non-constructor!
print("petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.printPetalCount();
}
}
常見用法:將this.s代表數據成員,防止和參數發生歧義。
注意點:
- this只能調用一個構造器
- this調用構造器必須將調用置於最開始處
- 除構造器之外,禁止在其他任何方法中調用構造器
static的含義:
- static方法的內部不能調用非靜態方法,例如this..
- 可以在沒有創建任何對象的前提下,僅僅通過類本身來調用static方法(這是static的主要用途)
- static被認爲不是“面向對象”的,因爲不存在this,所有不是通過“向對象發送消息”的方式來完成,若代碼中大量出現static方法,可以重新考慮自己的設計了。
static靜態不能引用非靜態方法的例子:
public class Person {
String nameString;;
void talk(){
// static void talk(){ 不可以使用,
// 因爲不能調用nameString,nameString屬於非靜態的成員變量。
System.out.println(nameString);
}
}
爲什麼不可以引用呢? why
容易理解的方式:
直接調用的方法,沒有創建對象。
this是指調用方法的那個對象,所以this沒法解釋,所以不能使用。若想引用方法只能引用靜態的,所以在把name成員變量改爲static類型就可以了。