java字

Java关键字this和super的理解?Java关键字this、super使用总结一、this Java关键字this只能用于方法方法体内。当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是this。因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现thi...

Java关键字this和super的理解?

Java关键字this、super使用总结

一、this Java关键字this只能用于方法方法体内。当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是this。因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,这在“Java关键字static、final使用总结”一文中给出了明确解释。并且this只和特定的对象关联,而不和类关联,同一个类的不同对象有不同的this。下面给出一个使用this的综合实例,以便说明问题:

package org.leizhimin;public class Test6 { private int number; private String username; private String password; private int x = 100; public Test6(int n) { number = n; // 这个还可以写为: this.number=n; } public Test6(int i, String username, String password) { // 成员变量和参数同名,成员变量被屏蔽,用"this.成员变量"的方式访问成员变量. this.username = username; this.password = password; } // 默认不带参数的构造方法 public Test6() { this(0, "未知", "空"); // 通过this调用另一个构造方法 } public Test6(String name) { this(1, name, "空"); // 通过this调用另一个构造方法 } public static void main(String args[]) { Test6 t1 = new Test6(); Test6 t2 = new Test6("游客"); t1.outinfo(t1); t2.outinfo(t2); } private void outinfo(Test6 t) { System.out.println("-----------"); System.out.println(t.number); System.out.println(t.username); System.out.println(t.password); f(); // 这个可以写为: this.f(); } private void f() { // 局部变量与成员变量同名,成员变量被屏蔽,用"this.成员变量"的方式访问成员变量. int x; x = this.x++; System.out.println(x); System.out.println(this.x); } //返回当前实例的引用 private Test6 getSelf() { return this; }}