欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中 this和super的用法與區(qū)別小結(jié)

 更新時(shí)間:2023年12月22日 11:23:31   作者:豪仔思密達(dá)  
在Java的學(xué)習(xí)與開發(fā)者我們經(jīng)常遇到this和super關(guān)鍵字,本文主要介紹了Java中 this和super的用法與區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下

在Java的學(xué)習(xí)與開發(fā)者我們經(jīng)常遇到thissuper關(guān)鍵字,那么它們的用法和區(qū)別是什么呢?

一、this關(guān)鍵字

1.this是什么?

this 是自身的一個(gè)對(duì)象,代表對(duì)象本身,可以理解為:指向?qū)ο蟊旧淼囊粋€(gè)指針。

2.this的用法

this 的用法在 Java 中大體可以分為3種:

(1)普通的直接引用

最簡(jiǎn)單的用法,this 相當(dāng)于是指向當(dāng)前對(duì)象本身。

(2)形參與成員名字重名,用 this 來區(qū)分:

示例:

public class Animal {

	public String name;
	public int health;
	public int love;
	
	public Animal() {無參構(gòu)造
	}

	public Animal(String name, int health, int love) {//有參構(gòu)造方法
		this.name = name;
		this.health = health;
		this.love = love;
	}
}

在創(chuàng)建一個(gè)類,并且聲明許多屬性后,我們一般都要為這個(gè)類創(chuàng)建無參和有參構(gòu)造方法,而在有參方法的參數(shù)列表中,我們會(huì)發(fā)現(xiàn)形參的名字與屬性名相同,這時(shí)我們就需要用this關(guān)鍵字來區(qū)分,不然就會(huì)無法實(shí)現(xiàn)我們想要的效果。

(3)引用構(gòu)造函數(shù)

注意:在一個(gè)構(gòu)造方法中通過this關(guān)鍵字調(diào)用其它構(gòu)造方法的時(shí)候,this關(guān)鍵字需要寫在構(gòu)造方法中的第一行位置。

示例:

public class ThisDemo01 {

	public String name;
	public String age;
	public ThisDemo01() {
		System.out.println("ThisDemo01類中的無參構(gòu)造方法");
	}

	public ThisDemo01(String name, String age) {
		//在一個(gè)構(gòu)造方法中通過this關(guān)鍵字調(diào)用其它構(gòu)造方法的時(shí)候,this關(guān)鍵字需要寫在構(gòu)造方法中的第一行位置
		this();
		System.out.println("ThisDemo01類中的有參構(gòu)造方法");
		//this();
		this.name = name;
		this.age = age;
		
	}
	
public static void main(String[] args) {
		
		ThisDemo01 thisDemo01 = new ThisDemo01();
		
		System.out.println("---------------");
		
		ThisDemo01 thisDemo012 = new ThisDemo01("張三", "22");

	}

}

結(jié)果:

 二、super關(guān)鍵字

1.super是什么?

super 可以理解為是指向自己父(根、超)類對(duì)象的一個(gè)指針,而這個(gè)父類指的是離自己最近的一個(gè)父類。

2.super的用法

super 也有三種用法:

(1)普通的直接引用

與 this 類似,super 相當(dāng)于是指向當(dāng)前對(duì)象的父類,這樣就可以用 super.xxx 來引用父類的成員。

(2)子類中的成員變量或方法與父類中的成員變量或方法同名:

class Country {
    String name;
    void value() {
       name = "China";
    }
}
  
class City extends Country {
    String name;
    void value() {
    name = "Shanghai";
    super.value();      //調(diào)用父類的方法
    System.out.println(name);
    System.out.println(super.name);
    }
  
    public static void main(String[] args) {
       City c=new City();
       c.value();
       }
}

結(jié)果:

Shanghai
China

可以看到,這里既調(diào)用了父類的方法,也調(diào)用了父類的變量。若不調(diào)用父類方法 value(),只調(diào)用父類變量 name 的話,則父類 name 值為默認(rèn)值 null。

(3)引用構(gòu)造函數(shù)

super(參數(shù)):調(diào)用父類中的某一個(gè)構(gòu)造函數(shù)(應(yīng)該為構(gòu)造函數(shù)中的第一條語句)。

//父類:
public class Animal{
	private String name;
	private int health;
	private int love;
	
	public Animal() {
	}

	public Animal(String name, int health, int love) {
		this.name = name;
		this.health = health;
		this.love = love;
	}

//子類:
public class Dog extends Animal{

	// 品種
	private String strain;

	public Dog() {
	}

	public Dog(String name, int health, int love, String strain) {
		super(name, health, love);
		this.strain = strain;
	}

由上述代碼可以看到,我們可以在子類的構(gòu)造方法中用super關(guān)鍵字調(diào)用父類具有同樣參數(shù)的構(gòu)造方法,減少代碼量。

(4)super的一些限制:

 super只能出現(xiàn)在子類的方法和構(gòu)造方法中 ;

super調(diào)用構(gòu)造方法時(shí),只能是第一句 ;

super不能訪問父類的private成員;

this() 和 super() 都指的是對(duì)象,所以,均不可以在 static 環(huán)境中使用。包括:static 變量,static 方法,static 語句塊。 

三、super和this關(guān)鍵字的異同

(1)super(參數(shù))調(diào)用基類中的某一個(gè)構(gòu)造函數(shù)(應(yīng)該為構(gòu)造函數(shù)中的第一條語句)

(2)this(參數(shù))調(diào)用本類中另一種形成的構(gòu)造函數(shù)(應(yīng)該為構(gòu)造函數(shù)中的第一條語句)

(3)super: 它引用當(dāng)前對(duì)象的直接父類中的成員(用來訪問直接父類中被隱藏的父類中成員數(shù)據(jù)或函數(shù),基類與派生類中有相同成員定義時(shí)如:super.變量名 super.成員函數(shù)據(jù)名(實(shí)參) this:它代表當(dāng)前對(duì)象名(在程序中易產(chǎn)生二義性之處,應(yīng)使用 this 來指明當(dāng)前對(duì)象;如果函數(shù)的形參與類中的成員數(shù)據(jù)同名,這時(shí)需用 this 來指明成員變量名)

(4)調(diào)用super()必須寫在子類構(gòu)造方法的第一行,否則編譯不通過。每個(gè)子類構(gòu)造方法的第一條語句,都是隱含地調(diào)用 super(),如果父類沒有這種形式的構(gòu)造函數(shù),那么在編譯的時(shí)候就會(huì)報(bào)錯(cuò)。

(5)super()  this() 類似,區(qū)別是,super() 從子類中調(diào)用父類的構(gòu)造方法,this() 在同一類內(nèi)調(diào)用其它方法。

(6)super() 和 this() 均需放在構(gòu)造方法內(nèi)第一行。

(7)盡管可以用this調(diào)用一個(gè)構(gòu)造器,但卻不能調(diào)用兩個(gè)。

(8)this 和 super 不能同時(shí)出現(xiàn)在一個(gè)構(gòu)造函數(shù)里面,因?yàn)閠his必然會(huì)調(diào)用其它的構(gòu)造函數(shù),其它的構(gòu)造函數(shù)必然也會(huì)有 super 語句的存在,所以在同一個(gè)構(gòu)造函數(shù)里面有相同的語句,就失去了語句的意義,編譯器也不會(huì)通過。

(9)this() 和 super() 都指的是對(duì)象,所以,均不可以在 static 環(huán)境中使用。包括:static 變量,static 方法,static 語句塊。

(10)從本質(zhì)上講,this 是一個(gè)指向本對(duì)象的指針, 然而 super 是一個(gè) Java 關(guān)鍵字。

到此這篇關(guān)于Java中 this和super的用法與區(qū)別小結(jié)的文章就介紹到這了,更多相關(guān)Java中 this和super內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論