用类名定义一个变量的时候,定义的应该只是一个引用,外面能够经过这个引用来访问这个类里面的属性和方法。类里面也有一个引用来访问本身的属性和方法,这个引用就是 this 对象,它能够在类里面来引用这个类的属性和方法。java
每当一个对象建立后,Java虚拟机会给这个对象分配一个引用自身的指针,这个指针的名字就是 this。所以,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this的用法。app
this主要要三种用法:ide
示例程序:函数
eg1工具
1 public class ThisDemo { 2 int number = 0; 3 ThisDemo increment(){ 4 number++; 5 return this; 6 } 7 private void print(){ 8 System.out.println("number="+number); 9 } 10 public static void main(String[] args) { 11 ThisDemo tt=new ThisDemo(); 12 tt.increment().increment().increment().print(); 13 } 14 } 15 /*Output: 16 i = 3 17 *///:~
能够看到,经过this关键字能够很容易在一条语句里面对同一个对象执行屡次操做。this
eg2 将当前的对象传递给其余方法spa
1 //: initialization/PassingThis.java 2 3 class Person { 4 public void eat(Apple apple) { 5 Apple peeled = apple.getPeeled(); 6 System.out.println("Yummy"); 7 } 8 } 9 10 class Peeler { 11 static Apple peel(Apple apple) { 12 // ... remove peel 13 return apple; // Peeled 14 } 15 } 16 17 class Apple { 18 Apple getPeeled() { return Peeler.peel(this); } 19 } 20 21 public class PassingThis { 22 public static void main(String[] args) { 23 new Person().eat(new Apple()); 24 } 25 } /* Output: 26 Yummy 27 *///:~
Apple须要调用Peeler.peel()方法,它是一个外部的工具方法,将执行因为某种缘由而必须放在Apple外部的操做。为了将其自身传递给外部方法,Apple必须使用this关键字。指针
eg3 在构造方法中引用知足指定参数类型的构造器code
1 //: initialization/Flower.java 2 // Calling constructors with "this" 3 import static net.mindview.util.Print.*; 4 5 public class Flower { 6 int petalCount = 0; 7 String s = "initial value"; 8 Flower(int petals) { 9 petalCount = petals; 10 print("Constructor w/ int arg only, petalCount= " 11 + petalCount); 12 } 13 Flower(String ss) { 14 print("Constructor w/ String arg only, s = " + ss); 15 s = ss; 16 } 17 Flower(String s, int petals) { 18 this(petals); 19 //! this(s); // Can't call two! 20 this.s = s; // Another use of "this" 参数s和数据成员s的名字相同,用this.s来避免歧义。 21 print("String & int args"); 22 } 23 Flower() { 24 this("hi", 47); 25 print("default constructor (no args)"); 26 } 27 void printPetalCount() { 28 //! this(11); // Not inside non-constructor! 29 print("petalCount = " + petalCount + " s = "+ s); 30 } 31 public static void main(String[] args) { 32 Flower x = new Flower(); 33 x.printPetalCount(); 34 } 35 } /* Output: 36 Constructor w/ int arg only, petalCount= 47 37 String & int args 38 default constructor (no args) 39 petalCount = 47 s = hi 40 *///:~
构造器Flower(String s, int petals)代表:尽管能够用this调用一个构造器,但却不能调用两个。此外,必须将构造器调用置于最起始位置处,不然编译器会报错。对象
printPetalCount()方法代表,除构造器以外,编译器禁止在其余任何方法中调用构造器。
super的使用和this基本相同,如今写下二者之间的比较:
1.super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。
2.this和super不能同时出如今一个构造函数里面。
3.super()和this()均需放在构造方法内第一行。
4.this()和super()都指的是对象,因此,均不能够在static环境中使用。
另外1.在构造器中this或super必须放在第一行
另外2(static的含义)
了解this关键字以后,就能更全面的理解static方法的含义。static方法没有this的方法。在static方法的内部不能调用非静态方法,但反过来能够。能够在没有建立任何对象的前提下,仅仅经过类自己来调用static方法。但这实际上正是static方法的主要用途。Java中禁止使用全局方法,但经过在类中置入static方法就能够访问其余static方法和static域。