您好,登錄后才能下訂單哦!
一.內(nèi)容簡(jiǎn)介
1.初始化與清理
2.訪問(wèn)權(quán)限
3.繼承概要
4.final,static
5.練習(xí)一
二.內(nèi)容梳理
1.初始化與清理
1.1構(gòu)造函數(shù)
構(gòu)造函數(shù):用來(lái)初始化成員變量的函數(shù),沒(méi)有返回值,函數(shù)名與類(lèi)名一致。
默認(rèn)構(gòu)造器:沒(méi)有參數(shù)的構(gòu)造函數(shù)。類(lèi)中如果沒(méi)有構(gòu)造器,編譯器會(huì)自動(dòng)創(chuàng)建
默認(rèn)構(gòu)造
器。
構(gòu)造函數(shù)的嵌套:可以在構(gòu)造器中調(diào)用被重載的其他構(gòu)造器,用
this(parameter
)調(diào)用。且只可以調(diào)用一個(gè)并放在起始處。
繼承中的構(gòu)造函數(shù):構(gòu)造函數(shù)不會(huì)被繼承,子類(lèi)的構(gòu)造函數(shù)自動(dòng)調(diào)用父類(lèi)的默認(rèn)構(gòu)造函數(shù),如果父類(lèi)沒(méi)有默認(rèn)構(gòu)造函數(shù),則手動(dòng)調(diào)用父類(lèi)的構(gòu)造函數(shù)。super(parameter).
1.2重載
重載:
方法名相同,參數(shù)列表不同。與返回值無(wú)關(guān),返回值無(wú)法區(qū)分,如果返回值不同,方法名相同,參數(shù)列表相同,編譯出錯(cuò)。
重載的參數(shù)
匹配:對(duì)應(yīng)類(lèi)型對(duì)應(yīng)匹配。如果匹配不到:
(常數(shù)與char
)當(dāng)做int用來(lái)
匹配,其他的數(shù)據(jù)類(lèi)型逐漸提升數(shù)據(jù)類(lèi)型來(lái)匹配
。
重寫(xiě)的返回類(lèi)型必須一樣或協(xié)變,否則編譯錯(cuò)誤。
1.3初始化
局部變量不初始化會(huì)報(bào)錯(cuò),成員變量有默認(rèn)的初始化。
初始化順序:靜態(tài)優(yōu)于非靜態(tài),父類(lèi)優(yōu)先子類(lèi),變量?jī)?yōu)先于代碼塊優(yōu)先于構(gòu)造器。
靜態(tài)數(shù)據(jù)的初始化:靜態(tài)數(shù)據(jù)屬于類(lèi),只占用一份存儲(chǔ)區(qū)域,有標(biāo)準(zhǔn)的初值。靜態(tài)數(shù)據(jù)的初始化時(shí)間是(靜態(tài)數(shù)據(jù)被訪問(wèn)或?qū)ο蟊粍?chuàng)建時(shí))
1.4清理
垃圾回收機(jī)制只釋放new的內(nèi)存
類(lèi)內(nèi)定義finalize()方法,當(dāng)垃圾回收發(fā)生時(shí),finalize()被調(diào)用
(5)可變參數(shù)列表
例子:
點(diǎn)擊( 此處 )折疊或打開(kāi)
static void printArray(Object[] args){
}
printArray(1, 2, 3);
printArray((Object[])new integer[](1, 2, 3));
——構(gòu)造函數(shù)使用可變參數(shù)列表會(huì)與默認(rèn)構(gòu)造函數(shù)弄混
2.訪問(wèn)權(quán)限
1.1
|
類(lèi)內(nèi) |
包內(nèi)(子類(lèi)與非子類(lèi)) |
包外子類(lèi) |
包外非子類(lèi) |
Public |
√ |
√ |
√ |
√ |
Protected |
√ |
√ |
√ |
× |
默認(rèn) |
√ |
√ |
× |
× |
private |
√ |
× |
× |
× |
1.2修飾類(lèi)只有public和默認(rèn)
3.繼承概要
extends指明繼承關(guān)系,未說(shuō)明隱式繼承Object.
向上轉(zhuǎn)型:子類(lèi)可以作為實(shí)參傳入父類(lèi)作為形參的方法。
this.value先找自己的成員,找不到去父類(lèi)找的.
構(gòu)造方法不會(huì)被繼承
4.final,static
1.1
final強(qiáng)調(diào)不變。static強(qiáng)調(diào)跟隨類(lèi)。
1.2final
類(lèi)不希望被繼承
方法不希望被重寫(xiě)
變量不希望被改變
final修飾的值不一定是常量還是變量,DJ反編譯看。
1.3static
靜態(tài)變量
靜態(tài)方法:
不能訪問(wèn)非靜態(tài)成員方法和非靜態(tài)成員變量
靜態(tài)代碼塊
static不允許修飾局部變量;
5.練習(xí)一
(1)需求
——汽車(chē)銷(xiāo)售人員銷(xiāo)售汽車(chē),款式有凱越,君威
——汽車(chē)與款式和編號(hào),款式編號(hào)不可修改
——汽車(chē)銷(xiāo)售人員有姓名
——銷(xiāo)售人員按車(chē)型及數(shù)量銷(xiāo)售
(2)代碼
AutoType.java
點(diǎn)擊( 此處 )折疊或打開(kāi)
package
biz
;
public
class
AutoType
{
public
static
final
String
KAI_YUE
=
"凱越"
;
public
static
final
String
JUB_WEI
=
"君威"
;
}
Auto.java
點(diǎn)擊( 此處 )折疊或打開(kāi)
package
biz
;
public
class
Auto
{
private
String
autoType
;
// 款式
private
String
autoNumber
;
// 編號(hào)
private
boolean
isSelled
;
Auto
(
String
autoType
,
String
autoNumber
)
{
this
.
autoType
=
autoType
;
this
.
autoNumber
=
autoNumber
;
this
.
isSelled
=
false
;
// 默認(rèn)未銷(xiāo)售
}
public
String
getAutoType
(
)
{
return
autoType
;
}
public
String
getAutoNumber
(
)
{
return
autoNumber
;
}
public
boolean
isSelled
(
)
{
return
isSelled
;
}
public
void
setSelled
(
boolean
isSelled
)
{
this
.
isSelled
=
isSelled
;
}
}
seller.java
點(diǎn)擊( 此處 )折疊或打開(kāi)
package
biz
;
public
class
Seller
{
private
String
name
;
Seller
(
String
name
)
{
this
.
name
=
name
;
}
/**
*
* @param autos 所用的車(chē)
* @param autoType 要銷(xiāo)售的類(lèi)型
* @param num 要銷(xiāo)售的數(shù)量
*/
public
void
sell
(
Auto
[
]
autos
,
String
autoType
,
int
num
)
{
// if(auto == null || autoType == null || num < 0) {
//
// }
int
selledNum
=
0
;
for
(
Auto curAuto
:
autos
)
{
int
i
=
0
;
i
+
+
;
if
(
!
curAuto
.
isSelled
(
)
&
&
curAuto
.
getAutoType
(
)
.
equals
(
autoType
)
&
&
selledNum
<
num
)
{
curAuto
.
setSelled
(
true
)
;
System
.
out
.
println
(
"準(zhǔn)備售出-"
+
curAuto
.
getAutoType
(
)
+
"-"
+
curAuto
.
getAutoNumber
(
)
)
;
selledNum
+
+
;
}
}
if
(
selledNum
<
num
)
{
System
.
out
.
println
(
"庫(kù)存不夠。"
+
"只可售出"
+
selledNum
+
"輛"
+
autoType
)
;
}
}
public
String
getName
(
)
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
static
void
main
(
String
[
]
args
)
{
Seller s1
=
new
Seller
(
"小明"
)
;
Auto auto1
=
new
Auto
(
AutoType
.
JUB_WEI
,
"001"
)
;
Auto auto2
=
new
Auto
(
AutoType
.
JUB_WEI
,
"002"
)
;
Auto
[
]
autos
=
new
Auto
[
2
]
;
autos
[
]
=
auto1
;
autos
[
1
]
=
auto2
;
s1
.
sell
(
autos
,
AutoType
.
JUB_WEI
,
3
)
;
}
}
三.總結(jié)與隨筆
(1)entity層:只有屬性沒(méi)有方法,主要用于數(shù)據(jù)跨層傳輸
(2)同一塊作用域,局部變量?jī)?yōu)先于成員變量。
(3)char 類(lèi)型可以' + ', ' - '
(4)方法傳參:基本類(lèi)型傳值,函數(shù)內(nèi)改變參數(shù)值對(duì)函數(shù)外無(wú)影響。引用類(lèi)型傳引用,函數(shù)內(nèi)改變參數(shù)值對(duì)函數(shù)外有影響。
(5)刪掉set方法,private修飾的成員變量只讀
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。