this关键字
如果有同一类型的两个对象,分别是a和b。你可能想知道,如何才能让这两个对象都能调用peel()方法呢:
//: initialization/BananaPeel.java 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); } } ///:~
如果只有一个peel()方法,它如何知道是被a还是被b所调用的呢?
为了能用简便、面向对象的语法来编写代码——即”发送消息给对象“,编译器做了一些幕后工作。
它暗自把”所操作对象的引用“作为第一个参数传递给peel().
所以上述两个方法的调用就编程了这样:
Banana.peel(a,1);
Banana.peel(b,2);
这是内部的表示形式。
我们并不能这样书写代码,并试图通过编译;
但这种写法的确能帮你了解实际所发生的事情。
假设你希望在方法的内部获得对当前对象的引用。
由于这个引用是由编译器”偷偷“传入的,所以没有标识符可用。
但是,为此有 个专门的关键字:this。
this关键字只能在方法内部使用,表示对”调用方法的那个对象“的引用。
this的用法和其他对象引用并无不同。
但是要注意,如果在方法内部调用同一个类的另一个方法,就不必使用this,直接调用即可。
当前方法中的this引用会自动应用于同一类中的其他方法。所以可以这样写代码:
//: initialization/Apricot.java public class Apricot { void pick() { /* ... */ } void pit() { pick(); /* ... */ } } ///:~
在pit()内部,你可以写this.pick(),但无此必要。
编译器能帮你自动添加。只要当需要明确指出当前对象的引用时,才需要使用this关键字。
例如,当需要返回对当前对象的引用时,就常常在return语句里这样写:
//: initialization/Leaf.java // Simple use of the "this" keyword. 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(); } } /* Output: i = 3 *///:~
由于increment()通过this关键字返回了对当前对象的引用,
所以很容易在一条语句里对同一个对象执行多次操作。
this关键字对于将当前对象传递给其他方法也很有用:
//: initialization/PassingThis.java 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()); } } /* Output: Yummy *///:~
Apple需要调用Peeler.peel()方法,他是一个外部的工具方法,将执行由于某种原因而必须放在Apple外部的操作(也许是因为该外部方法要应用于许多不同的类,而你却不想重复这些代码)。
为了将其自身传递给我不方法,Apple必须使用this关键字。
Calling constructors from constructors
在构造器中调用构造器
可能为一个类写了多个构造器,有时可能想在一个构造器中调用另一个构造器,以避免重复代码。
可用this关键字做到这一点。
通常写this的时候,都是指“这个对象”或者“当前对象”,而且它本身表示对当前对象的引用
在构造器中,如果为this添加了参数列表,那么就有了不同的含义。
这将产生对符合此参数列表的某个构造器的明确调用;这样,代用其他构造器就有了直接的途径:
//: initialization/Flower.java // Calling constructors with "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(); } } /* Output: Constructor w/ int arg only, petalCount= 47 String & int args default constructor (no args) petalCount = 47 s = hi *///:~
构造器Flower(String s,int petals)表明:尽管可以用this调用一个构造器,但却不能调用两个。
此外,必须将构造器调用置于最起始处,否则编译器会报错。
这个例子也展示了this的另一种用法。
由于参数s的名称和数据成员s的名字相同,所以会产生歧义。
使用this.s来代表数据成员就能解决这个问题。
在Java程序代码中经常出现这种写法,本书也常这么写
printPetalCount()方法表明,除构造器之外,编译器禁止在其他任何方法中调用构造器。
The meaning of static
static的含义
了解this关键字之后,就能更全面的理解static(静态)方法的含义。
static方法就是没有this的方法。
在static方法的内部不能调用非静态方法,反过来倒是可以的。
而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法。
这实际上正是static方法的主要用途。
它很像全局方法。
Java中禁止使用全局方法,但你在类中置入static方法就可以访问其他static方法和static域。
有些人认为static方法不是”面向对象”的,因为他们的确具有全局函数的语义;
使用static方法时,由于不存在this,所以不是通过“向对象发送消息”的方式来完成。
dique,要是在代码中出现了大量的static方法,就该重新考虑自己的设计了。
然而,static的概念有其实用之处,许多时候都要用到它。
至于它是否真的“面向对象”,就留给理论家去讨论吧。
事实上,Smalltalk语言里的“类方法”就是与static方法相对应的。