天天看點

Java中很有名的關鍵字——this,super1.this 2.super

1.this

this:本類對象的引用---其實this無處不在

1)在類内部調用屬性與方法

調用屬性與方法有兩種模式:

1.用該類的對象從外部調用。

2.使用this從類内部調用。

在類中直接調用的方法預設都是由this調用。

public class Test {
	public String name;
	public int age;
	public Test() {
	}
	public Test(String name, int age) {
		//直接調用方法,相當于this.set()
		set();
		this.name = name;
		this.age = age;
	}
	public void set() {}
	public static void main(String[] args) {
		Test t=new Test();
	}
}
           

 在類中直接調用的屬性預設都是由this調用。

public class Test {
	public String name;
	public int age;
	public Test() {
            //此處相當于this.age
		age=2;
	}
	public Test(String name, int age) {
		this.name = name;
		this.age = age;
		System.out.println("我是有參");
	}
	public void set() {}
	public static void main(String[] args) {
		Test t=new Test();
		System.out.println(t.age);
	}
}
輸出結果age=2;
           
2)this用來區分執行個體變量與局部變量
public class Test {
    //聲明執行個體變量
	public String name;
    //建立方法
	public void set(String name) {
		this.name=name;
	}
	public static void main(String[] args) {
    //建立對象
		Test a=new Test();
    //調用方法
		a.set("we");
		System.out.println(a.name);
	}
}
           

按照以上代碼,輸出為“we”。

this.name=name;this代表對象,this.調用屬性也就是執行個體變量,傳進來的name的值指派給執行個體變量,此時用a調用該變量,輸出name的值。

假設我們沒有寫this,輸出結果為null;

java中有就近原則,方法體中所有的變量會優先在方法内尋找是否有同名的,在此代碼中,不寫this時,所有的name都代表參數清單中的name,而不會具體改變執行個體變量的值,參數清單中的值會是“we”。

3)調用構造器

this可以用來在構造方法中調用其他的構造方法,格式:this()規定:必須放在方法體的第一條語句中。

public class Test {
	public String name;
	public int age;
	public Test() {
		System.out.println("我是無參");
	}
	public Test(String name, int age) {
		//調用無參構造
		this();
		this.name = name;
		this.age = age;
		System.out.println("我是有參");
	}
	public static void main(String[] args) {
		Test t=new Test("王二",20);
	}
}
           
public class Test {
	public String name;
	public int age;
	//調用有參構造
	public Test() {
		this("",0);
		System.out.println("我是無參");
	}
	public Test(String name, int age) {
		this.name = name;
		this.age = age;
		System.out.println("我是有參");
	}
	public static void main(String[] args) {
		Test t=new Test();
	}
}
           

 2.super

super:父類對象的引用---其實super無處不在

1)super調用父類的屬性與方法

調用父類方法:

public class Test {
	public String name;
	//父類無參構造
	public Test() {}
	//父類方法
	public void get() {
		System.out.println("我是父類");
	}
    //父類有參構造
	public Test(String name) {
		super();
		this.name = name;
	}
	public static void main(String[] args) {
		//建立子類對象
		Test1 t=new Test1();
		//調用子類方法
		t.set();
	}
	
}
class Test1 extends Test{
	//子類無參構造
    public Test1(){}
    //子類有參構造
	public Test1(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
	//子類方法---在方法中調用父類方法
	public void set() {
	get();//相當于super.get()
	}
}
           

調用父類屬性:

public class Test {
	public String name;
	//父類無參構造
	public Test() {}
    //父類有參構造
	public Test(String name) {
		super();
		this.name = name;
	}
	public static void main(String[] args) {
		//建立子類對象
		Test1 t=new Test1();
		t.set();
		System.out.println(t.name);
	}
	
}
class Test1 extends Test{
	//子類無參構造
    public Test1(){}
    //子類有參構造
	public Test1(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
	public void set() {
		//此時相當于super.name
	name="alex";
	}
}
           
2)在子類構造器中調用父類構造器

super可以用來在子類構造方法中調用父類的構造方法,格式:super()規定:必須放在方法體的第一條語句中。

注意:(一)

在前面提到過,this()隻可以放在方法體中第一句,是以,this()與super()不可以同時使用。

public class Test {
	public String name;
	//父類無參構造
	public Test() {
		System.out.println("我是父類無參");
	}
    //父類有參構造
	public Test(String name) {
		super();
		this.name = name;
		System.out.println("我是父類有參");
	}
	public static void main(String[] args) {
		//建立子類對象
		Test1 t=new Test1();
	}
	
}
class Test1 extends Test{
	//子類無參構造
    public Test1() {    	
    	super("");
    }
    //子類有參構造
	public Test1(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
}
輸出結果為:我是父類有參
           

注意:(二)

this 與 super 都是分别是本類對象與父類對象的引用,簡而言之,都是對象的引用,都是非靜态的。由于加載與執行順序等一些列問題,(有關靜态的問題過段時間總結)this與super不能在靜态成員中使用。

注意:(三)

this關鍵字不能在super之前使用,代碼如下

public class Teacher extends Worker{
	public int a=0;
        public Teacher() {}
        //從子類繼承的屬性
	public Teacher(int salary, String id, String name,int a) {
        //此處使用this關鍵字将會報錯,
		//this.a=a;
		super(salary, id, name);
		// TODO Auto-generated constructor stub
	}
	public void count(int salary) {
		System.out.println("教師的工資為:"+(salary+280));
	}
	
}
           

原因:this在這時是子類對象的引用,super是父類對象的引用,初始化的時候,父類進行初始化之後子類才可以進行初始化,super之前。子類沒有初始化,是以不可以使用this關鍵字。

注意:(四)

this可以直接輸出,而super不可以直接輸出。

public class Test {
	public Test() {}
	public void shuchu() {
		System.out.println(this);
	}
    public static void main(String[] args) {
		Test t=new Test();
		t.shuchu();
	}	
}
輸出結果:[email protected]
           
public class Test {
	public Test() {}
	public void shuchu() {
		System.out.println(this);
		//這樣寫會報錯
		System.out.println(super);
	}
    public static void main(String[] args) {
		Test t=new Test();
		t.shuchu();

	}	
}
class Test1 extends Test{
	
	
}
           

小白筆記--不足請指正。。。。