第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

子類用super調(diào)用父類有參數(shù)構(gòu)造函數(shù),是否會訪問到父類的私有屬性?

public?class?Car{
????private?int?carMoney;????//汽車租金
????private?String?carName;???//汽車名字
????private?int??carNumb;??????//汽車序列號
????private?int?carryNum;????//載貨量
????private?int?passengerNum;//載客人數(shù)
????
	public?Car(int?carNumb,String?carName,int??carMoney,int?passengerNum,int?carryNum){
		this.carMoney?=?carMoney;
		this.carName?=?carName;
		this.carNumb?=?carNumb;
		this.carryNum?=?carryNum;
		this.passengerNum?=?passengerNum;
	}
????public?int?getCarMoney()?{
????????return?carMoney;
????}
????public?void?setCarMoney(int?carMoney)?{
????????this.carMoney?=?carMoney;
????}
????public?String?getCarName()?{
????????return?carName;
????}
????public?void?setCarName(String?carName)?{
????????this.carName?=?carName;
????}
????public?int?getCarNumb()?{
????????return?carNumb;
????}
????public?void?setCarNumb(int?carNumb)?{
????????this.carNumb?=?carNumb;
????}
????public?int?getCarryNum()?{
????????return?carryNum;
????}
????public?void?setCarryNum(int?carryNum)?{
????????this.carryNum?=?carryNum;
????}
????public?int?getPassengerNum()?{
????????return?passengerNum;
????}
????public?void?setPassengerNum(int?passengerNum)?{
????????this.passengerNum?=?passengerNum;
????}
?
?
????public?void?showInfo()?{
????//?TODO?Auto-generated?method?stub
????}
?
????}
?
class?Sedan?extends?Car{
	public?Sedan(int?carNumb,String?carName,int??carMoney,int?passengerNum,int?carryNum){
		super(carNumb,carName,carMoney,passengerNum,carryNum);
	}
}

class?Pickup?extends?Car{
	public?Pickup(int?carNumb,String?carName,int??carMoney,int?passengerNum,int?carryNum){
		super(carNumb,carName,carMoney,passengerNum,carryNum);
	}
}

如上所示,不是說私有屬性無法繼承嗎?那么用super調(diào)用構(gòu)造函數(shù)時初始化的是什么?子類沒有繼承父類的屬性啊??墒怯靡陨洗a卻可以調(diào)用子類的構(gòu)造方法初始化父類的屬性。怎么回事?

正在回答

3 回答

super調(diào)用的這段代碼:

public?Car(int?carNumb,String?carName,int??carMoney,int?passengerNum,int?carryNum){
????????this.carMoney?=?carMoney;
????????this.carName?=?carName;
????????this.carNumb?=?carNumb;
????????this.carryNum?=?carryNum;
????????this.passengerNum?=?passengerNum;
????}

這段代碼中的this,如果你是創(chuàng)建Sedan,那么this就是Sedan,如果是Pickup,那么this 就是Pickup。

雖然父類的字段是private,但你提供了構(gòu)造方法去訪問。

0 回復(fù) 有任何疑惑可以回復(fù)我~

同學(xué),怎樣在這里提問

0 回復(fù) 有任何疑惑可以回復(fù)我~

恩,謝謝。這兩天又學(xué)習(xí)了一下,弄清楚了,發(fā)現(xiàn)我其實(shí)搞復(fù)雜了,搞重復(fù)了。其實(shí)在子類當(dāng)中聲明有參構(gòu)造器,直接在構(gòu)造器當(dāng)中用this調(diào)用set方法賦值就OK了。

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

子類用super調(diào)用父類有參數(shù)構(gòu)造函數(shù),是否會訪問到父類的私有屬性?

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號