Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2559646
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-11-17 09:26:39

This and Super

When writing code in the instance (or non-static) context of a class, the keyword this can be used to signify the instance of the class currently executing that code.

For example, the following code snippet defines a class Human that has a field age and a constructor that initializes the age field:


  1. public class Human {

  2.     public int age; 
  3.     public Human(int age) {
  4.       this.age = age;
  5.     }
  6. }

The parameter age shadows the field age in the scope of the constructor. If instead we were in a context outside of Human, say in class World, and we had a reference to an instance of Human, we could simply access the field age using the `.' operator:

  1. class World {
  2. ...
  3.  void someMethod() {
  4.  Human h = new Human(5);
  5.  System.out.print(h.age);
  6.  //prints 5
  7.   }
  8. }
However, inside the Human class context we do not have a handle on that reference. We know we are executing code for an instance, but how can we reference that instance? How can the object reference itself? Hence the keyword this.
然而,在Human类的上下文中,我们没有一个参考句柄,关键词this就是用于对象引用它自身。

The keyword super references the instance's superclass. Note that super and this can be used to access methods as well as fields. For example:

  1. public class Human {

  2.   private String name;
  3.   public Human(String name) {
  4.    this.name = name;
  5.    }
  6.  public String getName() {
  7.   return String;
  8.   }
  9. }

  1. public class Student extends Human {
  2.   private String username;
  3.   public Student(String name, String username) {
  4.    super(name);
  5.    this.username = username;
  6.   }
  7.  public String getName() {
  8.    return username;
  9.  }
  10.  public String getRealName() {
  11.    return super.getName();
  12.   }
  13. }

  1. public class World { 
  2.  ... 
  3.  void someMethod() { 
  4.   Student alice = new Student("Alice", "abc"); 
  5.   System.out.println(alice.getRealName());
In the above code snippet a Student is a Human with an extra field, username. Student overrides Human's getName method to return the username instead of name. However, the Human name can be accessed through another method, getRealName(), which calls the super's method, getName(). Thus, "Alice" is printed in World's someMethod(). If Human's name field were protected, then getRealName could return super.name, or simply name (since name has not been shadowed by a local variable or field in Student, the compiler knows to use the field in Human). The important point is that failure to use super will call the Student's getName() method, which would incorrectly print "abc".

Use super and this when you have to access fields and methods of the instance. However, when no shadowing occurs, for example in Student's getName() method, this is implicit and can be left off.

Finally, note the use of super in Student's constructor to call Human's constructor. The first line of a subclass's constructor must call the superclasses constructor. We wouldn't want the Student to have to initialize the Human's fields, so this makes sense.

Classes can contain multiple constructors, so use this to access a constructor of the same class.
  1. public class Student extends Human { private String username;
  2.  public Student(String name, String username) {
  3.   super(name);
  4.   this.username = username;
  5.  }
  6.  public Student(String name) {
  7.   this(name, name);
  8.  }
  9.  public Student() { 
  10.    this("Anonymous");
  11.  }

  12. }
In the above code snippet, Student has three constructors: one that takes a name and username, one that takes a name, and one that takes nothing. Remember, when overloading methods, the methods have the same name but different parameters.

Suppose we instantiate a student using the constructor that takes a single argument:
Student sting = new Student("Sting");

Execution first enters the single argument constructor, which calls the double argument constructor with "Sting" as both parameters. Thus, a Student is instantiated with name and username both equal to Sting.
Likewise, Student anon = new Student(); constructs a student with "Anonymous" as both the username and name.





阅读(822) | 评论(0) | 转发(0) |
0

上一篇:Java Iterator

下一篇:10个常用的正则表达式

给主人留下些什么吧!~~