this与super关键字总结

 Ⅰ.this

  用类名定义一个变量的时候,定义的应该只是一个引用,外面能够经过这个引用来访问这个类里面的属性和方法。类里面也有一个引用来访问本身的属性和方法,这个引用就是 this 对象,它能够在类里面来引用这个类的属性和方法。java

  每当一个对象建立后,Java虚拟机会给这个对象分配一个引用自身的指针,这个指针的名字就是 this。所以,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this的用法。app

  this主要要三种用法:ide

  1.   在类的方法定义中使用的this关键字表明调用该方法对象的引用。(this关键字只能在方法内部使用;只有当须要明确指出当前对象的引用时,才使用this关键字)
  2.   表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特例,比较经常使用,因此拿出来强调一下。
  3.   将当前的对象传递给其余方法。
  4.   用于在构造方法中引用知足指定参数类型的构造器(其实也就是构造方法)。可是这里必须很是注意:只能引用一个构造方法且必须位于开始!

示例程序:函数

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

  super的使用和this基本相同,如今写下二者之间的比较:

  1.super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。

  2.this和super不能同时出如今一个构造函数里面。

  3.super()和this()均需放在构造方法内第一行。

  4.this()和super()都指的是对象,因此,均不能够在static环境中使用。

 

另外1.在构造器中this或super必须放在第一行 

  必须在构造器的第一行放置super或者this构造器,不然编译器会自动地放一个空参数的super构造器的,其余的构造器也能够调用super或者this,调用成一个递归构造链,最后的结果是父类的构造器(可能有多级父类构造器)始终在子类的构造器以前执行,递归的调用父类构造器。没法执行当前的类的构造器。也就不能实例化任何对象,这个类就成为一个无为类。
   从另一面说,子类是从父类继承而来,继承了父类的属性和方法,若是在子类中先不完成父类的成员的初始化,则子类没法使用,应为在java中不容许调用没初始化的成员。在构造器中是顺序执行的,也就是说必须在第一行进行父类的初始化。而super能直接完成这个功能。This()经过调用本类中的其余构造器也能完成这个功能。所以,this()或者super()必须放在第一行。
 

另外2(static的含义)

  了解this关键字以后,就能更全面的理解static方法的含义。static方法没有this的方法。在static方法的内部不能调用非静态方法,但反过来能够。能够在没有建立任何对象的前提下,仅仅经过类自己来调用static方法。但这实际上正是static方法的主要用途。Java中禁止使用全局方法,但经过在类中置入static方法就能够访问其余static方法和static域。

相关文章
相关标签/搜索