那些年做的java基礎(chǔ)筆記

# 基本語(yǔ)法

## java基本數(shù)據(jù)類(lèi)型

1. 數(shù)值型

* 整數(shù)類(lèi)型:byte(8)、short(16)、int(32)、long(64)

* 浮點(diǎn)類(lèi)型:float、double <strong>(double,雙精度數(shù)值,表示的數(shù)據(jù)類(lèi)型是float類(lèi)型的雙倍)</strong>

2. 字符型:char ('a')

3. 布爾型:boolean (true,false)

注意:

1. JAVA默認(rèn)正數(shù)類(lèi)型是int類(lèi)型,如果需要聲明正數(shù)是long類(lèi)型,必須在數(shù)字末尾添加字母“L”

long lon = -1245L;

2. float類(lèi)型數(shù)值要以F作為后綴,沒(méi)有后綴,默認(rèn)為double類(lèi)型;

## 數(shù)據(jù)類(lèi)型轉(zhuǎn)換

1. <strong>自動(dòng)類(lèi)型轉(zhuǎn)換:低精度向高精度類(lèi)型轉(zhuǎn)換,適合于整數(shù)類(lèi)型和浮點(diǎn)類(lèi)型</strong>

byte類(lèi)型-short類(lèi)型-int類(lèi)型-long類(lèi)型-float類(lèi)型-double類(lèi)型

2. <strong>強(qiáng)制類(lèi)型轉(zhuǎn)換:高精度向低精度類(lèi)型轉(zhuǎn)換</strong>

注意:boolean不能被強(qiáng)制轉(zhuǎn)換為其他數(shù)據(jù)類(lèi)型,反之亦然

## 賦值運(yùn)算符

自增自減運(yùn)算符:

++X:先將變量X的值+1,然后再引用該變量的值參與運(yùn)算

X++:先將引用的變量參與表達(dá)式的運(yùn)算,然后再將其做+1處理

```java

public calss Operation{

? ? public static void main(String[] args){

? ? ? ? ? ? int m=5;

? ? ? ? ? ? int b=m++;

? ? ? ? ? ? System.out.println("變量b的值為:"+b);? // 5

? ? ? ? ? ? System.out.println("變量m的值為:"+m);? //6

? ? }

}

```

## 變量與常量

變量:type 標(biāo)識(shí)符? int age;

常量:final type 變量名稱(chēng)[=值]? final float PI=3.1415926f;

## 變量的有效范圍

1. 成員變量(全局變量):定義在類(lèi)體中,有效范圍是整個(gè)類(lèi)的代碼段。在類(lèi)體中的任何位置都可使用該變量。

? ? * 靜態(tài)變量:

? ? ? ? 1. 有效范圍是整個(gè)類(lèi),被類(lèi)的所有實(shí)例共享。

? ? ? ? 2. 使用static定義,調(diào)用變量,通過(guò)“類(lèi)名.變量名”方式訪(fǎng)問(wèn)。


? ? ? ? ```java

? ? ? ? public calss Variable{

? ? ? ? ? ? static int age=5;

? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? int m=5;

? ? ? ? ? ? ? ? int b=m++;

? ? ? ? ? ? ? ? System.out.println("靜態(tài)變量值為:"+Variable.age);? // 5

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? ```

? ? * 實(shí)例變量:

? ? ? ? 1. 與類(lèi)的實(shí)例對(duì)應(yīng),有效范圍為整個(gè)實(shí)例。


? ? ? ? ```java

? ? ? ? public calss Student{

? ? ? ? ? ? public static int amount=50;

? ? ? ? ? ? public int average=18;

? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? Student stu = new Student();

? ? ? ? ? ? ? ? System.out.println("班級(jí)人數(shù)為:"+Student.amount);? // 50

? ? ? ? ? ? ? ? System.out.println("平均年齡為:"+stu.average);? // 18

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? ```


2. 局部變量:

? ? * 局部變量只在當(dāng)前代碼塊才有效,不能用于其他類(lèi)的方法中。

? ? * 局部變量可與全局變量的變量名稱(chēng)相同,此時(shí)全局變量將被隱藏,可以使用“this”關(guān)鍵字訪(fǎng)問(wèn)局部變量。



? ? ```java

? ? ? ? public calss Student{

? ? ? ? ? ? static String words = "全局變量";

? ? ? ? ? ? public void getStu(){

? ? ? ? ? ? ? ? String words ="局部變量";

? ? ? ? ? ? ? ? System.out.println("words變量現(xiàn)在是:" + words);? // 局部變量

? ? ? ? ? ? ? ? System.out.println("訪(fǎng)問(wèn)全局變量:" + this.words);? // 全局變量

? ? ? ? ? ? }

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? Student stu = new Student();

? ? ? ? ? ? ? ? stu.getStu();

? ? ? ? ? ? }

? ? ? ? }

? ? ```


# 流程控制語(yǔ)句

## if條件判斷語(yǔ)句

1. 簡(jiǎn)單的if條件語(yǔ)句


? ? ```java

? ? ? ? if(關(guān)系表達(dá)式){

? ? ? ? ? ? 復(fù)合語(yǔ)句

? ? ? ? }

? ? ```

? ? ```java

? ? ? ? public calss IfExercise{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? float price=60.89f;

? ? ? ? ? ? ? ? if(price<100){

? ? ? ? ? ? ? ? System.out.println("價(jià)格可以接受");?

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? System.out.println("over");

? ? ? ? ? ? }

? ? ? ? }

? ? ```

2. if...else語(yǔ)句


? ? ```java

? ? ? ? if(條件表達(dá)式){

? ? ? ? ? ? 復(fù)合語(yǔ)句1

? ? ? ? }

? ? ? ? else{

? ? ? ? ? ? 復(fù)合語(yǔ)句2

? ? ? ? }

? ? ```

? ? ```java

? ? ? ? public calss IfElseExercise{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? float price=60.89f;

? ? ? ? ? ? ? ? if(price<100){

? ? ? ? ? ? ? ? System.out.println("價(jià)格可以接受");?

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? System.out.println("太貴了,拒絕購(gòu)買(mǎi)");?

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? System.out.println("over");

? ? ? ? ? ? }

? ? ? ? }

? ? ```

? ? <strong>注意:if...else語(yǔ)句可使用三元運(yùn)算符代替,if(price<100)?"價(jià)格可接受":"太貴了,拒絕購(gòu)買(mǎi)"</strong>?

3. if...else if多分支語(yǔ)句


? ? ```java

? ? ? ? if(條件表達(dá)式1){

? ? ? ? ? ? 復(fù)合語(yǔ)句1

? ? ? ? }

? ? ? ? else if(條件表達(dá)式2){

? ? ? ? ? ? 復(fù)合語(yǔ)句2

? ? ? ? }

? ? ? ? ...

? ? ? ? else if(條件表達(dá)式n){

? ? ? ? ? ? 復(fù)合語(yǔ)句n

? ? ? ? }

? ? ```


? ? ```java

? ? ? ? public calss Verdict{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? int number=14;

? ? ? ? ? ? ? ? if(number>20){

? ? ? ? ? ? ? ? System.out.println("變量number大于20");?

? ? ? ? ? ? ? ? }else if(number>10){

? ? ? ? ? ? ? ? System.out.println("變量number大于10小于20");?

? ? ? ? ? ? ? ? }else if(number>0){

? ? ? ? ? ? ? ? System.out.println("變量number大于0小于10");?

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? System.out.println("變量number小于等于0");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ```?

## switch語(yǔ)句

? ? ```java

? ? ? ? switch(條件表達(dá)式){

? ? ? ? ? ? case 常量表達(dá)式1:

? ? ? ? ? ? ? ? 語(yǔ)句塊1

? ? ? ? ? ? ? ? [break;]

? ? ? ? ? ? ...

? ? ? ? ? ? case 常量表達(dá)式n:

? ? ? ? ? ? ? ? 語(yǔ)句塊n

? ? ? ? ? ? ? ? [break;]

? ? ? ? ? ? default;

? ? ? ? ? ? ? ? 語(yǔ)句塊n+1;

? ? ? ? ? ? ? ? [break;]

? ? ? ? }

? ? ```?


? ? 1. 條件表達(dá)式為必要參數(shù);

? ? 2. 常量表達(dá)式必須與條件表達(dá)式類(lèi)型相兼容的值;

? ? 3. 語(yǔ)句塊:可選參數(shù),一條或多條語(yǔ)句,不需要大括號(hào)。當(dāng)表達(dá)式的值域常量表達(dá)式的值匹配時(shí)執(zhí)行,否則繼續(xù)判斷其他值,直到常量表達(dá)式n;

? ? 4. break:可選參數(shù),用于跳出switch語(yǔ)句;

? ? 5. default:可選參數(shù),如果沒(méi)有該語(yǔ)句,switch語(yǔ)句不與任何case語(yǔ)句常量值相同時(shí),不做任何處理。


? ? ```java

? ? ? ? public calss SwitchExercise{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? int week=3;

? ? ? ? ? ? ? ? switch(week){

? ? ? ? ? ? ? ? case 1:

? ? ? ? ? ? ? ? ? ? System.out.println("周一");

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 2:

? ? ? ? ? ? ? ? ? ? System.out.println("周二");

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 3:

? ? ? ? ? ? ? ? ? ? System.out.println("周三");

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? default:

? ? ? ? ? ? ? ? ? ? System.out.println("I dont't know");

? ? ? ? ? ? ? ? }

? ? ? ? }

? ? ```?

注意:如果不加break,則匹配之后,會(huì)繼續(xù)判斷并執(zhí)行其他case。比如,上述例子中,去除break之后,則會(huì)打印“周三”和"I dont't know"。

## 循環(huán)語(yǔ)句

1. for循環(huán)語(yǔ)句

? ? ```java

? ? ? ? for(初始化語(yǔ)句;循環(huán)語(yǔ)句;迭代語(yǔ)句){

? ? ? ? ? ? 語(yǔ)句序列

? ? ? ? }

? ? ```

? ? ```java

? ? ? ? public calss CountSum{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? int sum=0;

? ? ? ? ? ? ? ? for(int i=1;i<=100;i++){

? ? ? ? ? ? ? ? sum+=i;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? System.out.println("1~100的和為:"+sum);

? ? ? ? ? ? }

? ? ? ? }

? ? ```?

2. while循環(huán)語(yǔ)句:先進(jìn)行條件判斷,再執(zhí)行循環(huán)體中的內(nèi)容。

? ? ```java

? ? ? ? while(條件表達(dá)式){

? ? ? ? ? ? 循環(huán)體

? ? ? ? }

? ? ```

? ? ```java

? ? ? ? public calss GetSum{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? int x=1;

? ? ? ? ? ? ? ? int sum=0;

? ? ? ? ? ? ? ? while(x<=10){

? ? ? ? ? ? ? ? sum+=x;

? ? ? ? ? ? ? ? x++;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? System.out.println("1~10的和為:"+sum);

? ? ? ? ? ? }

? ? ? ? }

? ? ```

3. do...while循環(huán)語(yǔ)句:先執(zhí)行循環(huán)體中的內(nèi)容,再進(jìn)行條件判斷,如果條件不滿(mǎn)足,則不執(zhí)行循環(huán)體中內(nèi)容,即循環(huán)體中內(nèi)容至少執(zhí)行一次。

? ? ```java

? ? ? ? do{

? ? ? ? ? ? 語(yǔ)句序列? ? ? ?

? ? ? ? }while(條件表達(dá)式);

? ? ```

? ? ```java

? ? ? ? public calss Cycle{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? int number=100;

? ? ? ? ? ? ? ? while(number==60){

? ? ? ? ? ? ? ? ? ? System.out.println("使用while循環(huán)");

? ? ? ? ? ? ? ? ? ? number--;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? do{

? ? ? ? ? ? ? ? ? ? System.out.println("使用do...while循環(huán)");

? ? ? ? ? ? ? ? ? ? number--;

? ? ? ? ? ? ? ? }while(number==60);

? ? ? ? ? ? }

? ? ? ? }

? ? ```


? ? ## 結(jié)束本次循環(huán)的continue語(yǔ)句

? ? 1. continue語(yǔ)句用于終止當(dāng)前循環(huán),返回到循環(huán)開(kāi)始處,接著開(kāi)始新的循環(huán);

? ? 2. continue語(yǔ)句只能出現(xiàn)在循環(huán)語(yǔ)句:while、for、do...while語(yǔ)句中;


? ? ```java

? ? ? ? public calss Continue{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? System.out.println("可以被10整除的數(shù)有:");

? ? ? ? ? ? ? ? for(int i=1;i<=100;i++){

? ? ? ? ? ? ? ? ? ? if(i%10!=0){

? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? System.out.println(i+"、");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }? ? ? ?

? ? ? ? }

? ? ```


? ? ## 強(qiáng)行退出循環(huán)的break語(yǔ)句

? ? 1. 中途中斷循環(huán)執(zhí)行,可使用break強(qiáng)迫跳出循環(huán);

? ? 2. continue語(yǔ)句可出現(xiàn)在循環(huán)語(yǔ)句:while、for、do...while語(yǔ)句中;


? ? ```java

? ? ? ? public calss BreakPractice{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? int sum=0;

? ? ? ? ? ? ? ? int control=0;

? ? ? ? ? ? ? ? while(true){

? ? ? ? ? ? ? ? ? ? control++;

? ? ? ? ? ? ? ? ? ? if(control>100){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("1~100所有整數(shù)的和是:"+sum);

? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? sum+=control;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ```


? ? ## 循環(huán)中使用標(biāo)簽

? ? 1. 指定程序跳轉(zhuǎn)至哪一行。

? ? 2. 使用break只能退出當(dāng)前循環(huán),如果想通過(guò)break語(yǔ)句退出嵌套的循環(huán)時(shí),需要使用帶標(biāo)簽的break語(yǔ)句實(shí)現(xiàn)。

? ? 3. break和continue都可以配合標(biāo)簽使用。


? ? ```java

? ? ? ? 標(biāo)簽名稱(chēng):? //標(biāo)簽名稱(chēng)的命名規(guī)則與變量的命名規(guī)則一致,可添加到程序的任意位置

? ? ```


? ? ```java

? ? ? ? public calss Multiplication{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? loop:

? ? ? ? ? ? ? ? for(int i=1;i<=9;i++){

? ? ? ? ? ? ? ? ? ? for(int j=1;j<=i;j++){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println(i+"*"+j+"="+i*j+" ");

? ? ? ? ? ? ? ? ? ? ? ? if(j==5){

? ? ? ? ? ? ? ? ? ? ? ? ? ? break loop;

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? System.out.println();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ```


? ? 注意:


? ? 1. for循環(huán)主要針對(duì)有限循環(huán)而言,也就是說(shuō),循環(huán)有上限的時(shí)候,一般使用for循環(huán);

? ? 2. while循環(huán)則針對(duì)無(wú)限循環(huán)的代碼而言,當(dāng)程序沒(méi)有明確的上限,則一般使用while循環(huán)。

# 數(shù)組

## 一維數(shù)組

1. 創(chuàng)建數(shù)組的語(yǔ)法格式


? ? ```java

? ? ? ? 數(shù)組類(lèi)型 數(shù)組名[];

? ? ? ? 或

? ? ? ? 數(shù)組類(lèi)型[] 數(shù)組名;

? ? ```


? ? ```java

? ? ? ? // 如:

? ? ? ? int arr[];

? ? ? ? char arr[];

? ? ```


? ? ```java

? ? ? ? // 為數(shù)組分配內(nèi)存空間的格式如下:

? ? ? ? 數(shù)組名字 = new 數(shù)組類(lèi)型[數(shù)組長(zhǎng)度];


? ? ? ? // 如:

? ? ? ? int[] arr = new int[5];

? ? ```

2. 數(shù)組初始化


? ? ```java

? ? ? ? // 第一種初始化方式

? ? ? ? int arr[] = new int[] {1,3,5,7};


? ? ? ? // 第二種初始化方式

? ? ? ? int arr[] = {1,3,5,7};

? ? ```


? ? ```java

? ? ? ? int arr[] = new int[4];

? ? ? ? int arr[0]=1;

? ? ? ? int arr[1]=3;

? ? ? ? int arr[2]=5;

? ? ? ? int arr[3]=7;

? ? ```

3. 遍歷一維數(shù)組


? ? ```java

? ? ? ? public calss SearchArray{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? int day[]=new int[]{31,28,31,30,31};

? ? ? ? ? ? ? for(int i=0;i<5;i++){

? ? ? ? ? ? ? ? ? ? System.out.println((i+1)+"月有"+day[i]+"天");

? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ```

? ? ```java

? ? // length表示數(shù)組長(zhǎng)度

? ? ? ? public calss SearchArray{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? int day[]=new int[]{31,28,31,30,31};

? ? ? ? ? ? ? for(int i=0;i<day.length;i++){

? ? ? ? ? ? ? ? ? ? System.out.println((i+1)+"月有"+day[i]+"天");

? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ```


##二維數(shù)組? ?

1. 聲明數(shù)組對(duì)象的語(yǔ)法格式


? ? ```java

? ? ? ? 數(shù)組類(lèi)型 數(shù)組名[][];

? ? ? ? 或

? ? ? ? 數(shù)組類(lèi)型[][] 數(shù)組名;

? ? ```


? ? ```java

? ? ? ? // 如:

? ? ? ? int arr[][];

? ? ? ? char arr[][];

? ? ```


? ? ```java

? ? ? ? // 為數(shù)組分配內(nèi)存空間的格式如下:

? ? ? ? 數(shù)組名字 = new 數(shù)組類(lèi)型[數(shù)組長(zhǎng)度][數(shù)組長(zhǎng)度];


? ? ? ? // 如:

? ? ? ? int arr[][] = new int[5][5];

? ? ```


2. 二維數(shù)組初始化


? ? ```java

? ? ? ? 數(shù)組類(lèi)型 變量名稱(chēng) [][]={

? ? ? ? ? ? {value1,value2,value3...valuen},

? ? ? ? ? ? {value1,value2,value3...valuen}

? ? ? ? ? ? ...

? ? ? ? }

? ? ? ? // 如:

? ? ? ? int arr[][]={{23,12,2},{12,45}} ;


? ? ? ? // arr[1]的第一個(gè)元素賦值為12:

? ? ? ? arr[1][0]=12;

? ? ```

3. 使用嵌套的for循環(huán)語(yǔ)句遍歷二維數(shù)組


? ? ```java

? ? ? ? public calss CountMark{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? int grades[][]={

? ? ? ? ? ? ? {77,68,86,73},

? ? ? ? ? ? ? {96,87,89,81},

? ? ? ? ? ? ? {70,90,86,81}

? ? ? ? ? ? ? } ;

? ? ? ? ? ? ? int lowGrade =? grades[0][0];

? ? ? ? ? ? ? for(int i=0;i<grades.length;i++){

? ? ? ? ? ? ? ? ? ? for(int column=0;column<grades[i].length;column++){

? ? ? ? ? ? ? ? ? ? ? ? if(grades[i][column]<lowGrade)

? ? ? ? ? ? ? ? ? ? ? ? ? ? lowGrade=grades[i][column];

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? }?

? ? ? ? ? ? ? int highGrade =? grades[0][0];

? ? ? ? ? ? ? for(int j=0;j<grades.length;j++){

? ? ? ? ? ? ? ? ? ? for(int column=0;column<grades[j].length;column++){

? ? ? ? ? ? ? ? ? ? ? ? if(grades[j][column]>highGrade)

? ? ? ? ? ? ? ? ? ? ? ? ? ? highGrade=grades[j][column];

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? }?

? ? ? ? ? ? ? System.out.println("最低分?jǐn)?shù)為:"+lowGrade);

? ? ? ? ? ? ? System.out.println("最高分?jǐn)?shù)為:"+highGrade);?

? ? ? ? ? ? }? ?

? ? ? ? }

? ? ```


4. 使用嵌套的for循環(huán)語(yǔ)句遍歷二維數(shù)組


? ? ```java

? ? ? ? public calss CountMark{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? int grades[][]={

? ? ? ? ? ? ? {77,68,86,73},

? ? ? ? ? ? ? {96,87,89,81},

? ? ? ? ? ? ? {70,90,86,81}

? ? ? ? ? ? ? } ;

? ? ? ? ? ? ? int lowGrade =? grades[0][0];

? ? ? ? ? ? ? for(int i=0;i<grades.length;i++){

? ? ? ? ? ? ? ? ? ? for(int column=0;column<grades[i].length;column++){

? ? ? ? ? ? ? ? ? ? ? ? if(grades[i][column]<lowGrade)

? ? ? ? ? ? ? ? ? ? ? ? ? ? lowGrade=grades[i][column];

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? }?

? ? ? ? ? ? ? int highGrade =? grades[0][0];

? ? ? ? ? ? ? for(int j=0;j<grades.length;j++){

? ? ? ? ? ? ? ? ? ? for(int column=0;column<grades[j].length;column++){

? ? ? ? ? ? ? ? ? ? ? ? if(grades[j][column]>highGrade)

? ? ? ? ? ? ? ? ? ? ? ? ? ? highGrade=grades[j][column];

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? }?

? ? ? ? ? ? ? System.out.println("最低分?jǐn)?shù)為:"+lowGrade);

? ? ? ? ? ? ? System.out.println("最高分?jǐn)?shù)為:"+highGrade);?

? ? ? ? ? ? }? ?

? ? ? ? }

? ? ```


5. 使用foreach遍歷二維數(shù)組

? ? ```java

? ? ? ? for(元素變量:遍歷對(duì)象){

? ? ? ? ? ? 循環(huán)體

? ? ? ? }

? ? ```


? ? ```java

? ? ? ? // 一維數(shù)組遍歷

? ? ? ? int[] array={12,34,43,22,13,58};

? ? ? ? for(int i :array){

? ? ? ? ? ? System.out.println(i);

? ? ? ? }

? ? ```


? ? ```java

? ? ? ? // 使用嵌套foreach循環(huán)語(yǔ)句遍歷二維數(shù)組

? ? ? ? public calss ArrayForEach{

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? ? ? int array[][]={{1,2,3},{7,8,9},{13,14,15}};

? ? ? ? ? ? ? ? ? ? for(int[] is:array){

? ? ? ? ? ? ? ? ? ? ? ? for(int is2:is){

? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println(is2+"\t");

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? System.out.println();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }? ?

? ? ? ? }

? ? ```


## 使用sort() 方法實(shí)現(xiàn)數(shù)組排序?

1. 語(yǔ)法:

? ```java

? ? Arrays.sort(array)

? ```


2. example:

? ```java

? ? public static void main(String[] args){

? ? ? ? int arr[]=new int[]{61,30,52,40,12,21};

? ? ? ? System.out.println("聲明的數(shù)組原有內(nèi)容如下:");

? ? ? ? for (int i = 0; i < arr.length; i++){

? ? ? ? ? ? System.out.println(arr[i]+"\t");

? ? ? ? }

? ? ? ? System.out.println("\n排序后的數(shù)組內(nèi)容如下:");

? ? ? ? Arrays.sort(arr);

? ? ? ? for (int i = 0; i < arr.length; i++){

? ? ? ? ? ? System.out.println(arr[i]+"\t");

? ? ? ? }

? ? }

? ```


##復(fù)制數(shù)組

1. Arrarys類(lèi)的copyOf()方法:

? ```java

? ? // 語(yǔ)法

? ? copyOf(arr,int newlength)

? ? 注意:

? ? 如果新數(shù)組的長(zhǎng)度,大于數(shù)組arr的長(zhǎng)度,則用0填充。

? ? 根據(jù)復(fù)制數(shù)組的類(lèi)型來(lái)決定填充的值,整型數(shù)組用0填充,char型數(shù)組則會(huì)使用null填充。

? ? 如果復(fù)制后的數(shù)組長(zhǎng)度小于數(shù)組arr的長(zhǎng)度,會(huì)從數(shù)組arr的第一個(gè)元素開(kāi)始,截取至滿(mǎn)足新數(shù)組長(zhǎng)度為止。

? ```

? ```java

? ? public static void main(String[] args){

? ? ? ? int arr[]=new int[]{61,30,52};

? ? ? ? int newarr[]=Arrays.copyOf(arr,6);

? ? ? ? System.out.println("復(fù)制后新數(shù)組的元素為:");

? ? ? ? for (int i = 0; i < newarr.length; i++){

? ? ? ? ? ? System.out.println(newarr[i]+"");

? ? ? ? }

? ? }

? ```


2. Arrarys類(lèi)的copyOfRange()方法:

? ```java

? ? // 語(yǔ)法

? ? copyOfRange(arr,int formIndex,int toIndex)

? ? 注意:

? ? formIndex:開(kāi)始復(fù)制數(shù)組的索引位置,formIndex必須在0與整個(gè)數(shù)組的長(zhǎng)度之間。新數(shù)組包括索引是formIndex的元素。

? ? toIndex:復(fù)制范圍的最后索引位置,可以大于數(shù)組arr的長(zhǎng)度,新數(shù)組不包括索引是toIndex的元素。

? ```

? ```java

? ? public static void main(String[] args){

? ? ? ? int arr[]=new int[]{61,30,52,23,47};

? ? ? ? System.out.println("原數(shù)組的元素為:");

? ? ? ? for (int i = 0; i < arr.length; i++){

? ? ? ? ? ? System.out.println(arr[i]+"");

? ? ? ? }

? ? ? ? int newarr[]=Arrays.copyOfRange(arr,0,4);

? ? ? ? System.out.println("復(fù)制后的元素為:");

? ? ? ? for (int i = 0; i < newarr.length; i++){

? ? ? ? ? ? System.out.println(newarr[i]+"");

? ? ? ? }

? ? }

? ```


# 類(lèi)與對(duì)象

## 類(lèi)的定義

1. 聲明類(lèi)的具體語(yǔ)法格式:

? ? ``` java

? ? class className{

? ? ? ? 成員變量

? ? ? ? ...

? ? ? ? 成員方法

? ? ? ? ...

? ? }

? ? ```

? ? ``` java

? ? public class Person{

? ? ? ? int age;

? ? ? ? public void say(){

? ? ? ? System.out.println("在類(lèi)體中定義的方法");

? ? ? ? }

? ? }

? ? ```

## Java中的類(lèi)包

1. 如何定義包:

? ? ```java

? ? package 包名;

? ? ```


? ? 將類(lèi)Person定義在包c(diǎn)om.cdd.util中:


? ? ```java

? ? package com.cdd.util;

? ? public class Person{

? ? ? ? int age;

? ? ? ? ... // 省略類(lèi)體中的其他代碼

? ? }

? ? ```


2. 實(shí)現(xiàn)在類(lèi)中導(dǎo)入包:

在每個(gè)類(lèi)名之前添加完整的包名:

? ? ```java

? ? java.util.Date date=new java.util.Date();

? ? ```

import語(yǔ)句引入包中的類(lèi):

? ? ```java

? ? import com.wgh.Circ;

? ? ```

? ? ```java

? ? import com.wgh.*;

? ? ```


## Java中的類(lèi)

1. 成員方法

成員方法定義了Java類(lèi)的行為,類(lèi)的成員方法由方法的聲明和方法體兩部分組成。

? ? ```java

? ? [修飾符]<方法返回值的類(lèi)型><方法名>([參數(shù)列表]){

? ? ? ? [方法體]

? ? ? ? }

? ? ```


? ? ```java

? ? public class Person{

? ? ? ? public void print(){? ? ? ? //在類(lèi)中定義成員方法

? ? ? ? ? ? System.out.println("類(lèi)的成員方法??!");

? ? ? ? }

? ? }

? ? ```


2. 靜態(tài)方法

在使用成員變量和成員方法時(shí),必須通過(guò)對(duì)象才能訪(fǎng)問(wèn)。靜態(tài)方法則不同,直接通過(guò)類(lèi)名就能調(diào)用,或者通過(guò)類(lèi)的實(shí)例來(lái)調(diào)用,還可以在類(lèi)的非靜態(tài)方法中像訪(fǎng)問(wèn)其他非靜態(tài)方法一樣去訪(fǎng)問(wèn)靜態(tài)成員方法。

? ? ```java

? ? [修飾符]<方法返回值的類(lèi)型><方法名>([參數(shù)列表]){

? ? ? ? [方法體]

? ? }

? ? ```

? ? ```java

? ? public class Chinese {

? ? ? ? public static void say() {? ? ? //創(chuàng)建靜態(tài)方法

? ? ? ? ? ? System.out.println("JavaClass類(lèi)中的靜態(tài)方法!");

? ? ? ? }

? ? ? ? public void sing(){

? ? ? ? ? ? System.out.println("JavaClass類(lèi)中的非靜態(tài)方法!");

? ? ? ? }

? ? }

? ? ```

? ? ```java

? ? public class TestChinese {

? ? ? ? public static void main(String[] args){

? ? ? ? ? ? Chinese.say();? ? ? //調(diào)用Chinese類(lèi)的靜態(tài)方法

? ? ? ? ? ? Chinese javaClass =new Chinese();? //創(chuàng)建Chinese類(lèi)對(duì)象

? ? ? ? ? ? javaClass.sing();? //調(diào)用Chinese類(lèi)的非靜態(tài)方法

? ? ? ? ? ? javaClass.say();? ? //調(diào)用Chinese類(lèi)的靜態(tài)方法

? ? ? ? }

? ? }

? ? ```


3. 靜態(tài)塊

靜態(tài)塊是指不包含在任何方法體中的靜態(tài)代碼塊,應(yīng)用static關(guān)鍵字包圍。當(dāng)類(lèi)被加載時(shí),靜態(tài)塊就會(huì)被執(zhí)行且只被執(zhí)行一次。

? ? ```java

? ? public class Test {

? ? ? ? static{

? ? ? ? ? ? int age=32;

? ? ? ? ? ? System.out.println("int類(lèi)型變量中age的值為:"+age);

? ? ? ? }

? ? public static void main(String[] args){

? ? ? ? ? ? new Test();

? ? ? ? ? ? new Test();

? ? ? ? }

? ? }

? ? ```


4. 權(quán)限修飾符

private:成員變量只能在本類(lèi)中使用,在子類(lèi)中不可見(jiàn),并且對(duì)其他包的類(lèi)也是不可見(jiàn)的

public:不僅可以在本類(lèi)中使用,在子類(lèi)和其他包的類(lèi)也可以使用

protected:只有本包內(nèi)的該類(lèi)的子類(lèi)或其他類(lèi)可以訪(fǎng)問(wèn)此類(lèi)中的成員變量和成員方法

5. 方法重載

在一個(gè)類(lèi)中,出現(xiàn)多個(gè)方法名相同,但參數(shù)個(gè)數(shù)或者參數(shù)類(lèi)型不同的方法,Java在執(zhí)行具有重載關(guān)系的方法時(shí),將根據(jù)調(diào)用參數(shù)的個(gè)數(shù)和類(lèi)型區(qū)分具體執(zhí)行的是哪個(gè)方法。

? ? ```java

? ? public class Calculate {

? ? ? ? final float PI = 3.14159f;

? ? ? ? public float getArea(float r) {

? ? ? ? ? ? float area = PI * r * r;

? ? ? ? ? ? return area;

? ? ? ? }

? ? ? ? public float getArea(float l, float w) {

? ? ? ? ? ? float area = l * w;

? ? ? ? ? ? return area;

? ? ? ? }

? ? ? ? public void draw(int num) {

? ? ? ? ? ? System.out.println("畫(huà)" + num + "個(gè)任意形狀的圖形");

? ? ? ? }

? ? ? ? public void draw(String shape) {

? ? ? ? ? ? System.out.println("畫(huà)一個(gè)" + shape);

? ? ? ? }

? ? ? ? public static void main(String[] args) {

? ? ? ? ? ? Calculate calculate = new Calculate();

? ? ? ? ? ? float l = 20;

? ? ? ? ? ? float w = 30;

? ? ? ? ? ? float areaRectangle=calculate.getArea(l, w);

? ? ? ? ? ? System.out.println("長(zhǎng)為"+l+",寬為"+w+",的矩形面積是:"+areaRectangle);


? ? ? ? ? ? float r=7;

? ? ? ? ? ? float areacirle=calculate.getArea(r);

? ? ? ? ? ? System.out.println("半徑為"+r+",的圓形面積是:"+areacirle);

? ? ? ? ? ? int num=7;

? ? ? ? ? ? calculate.draw(num);;

? ? ? ? ? ? calculate.draw("三角形");

? ? ? ? }

? ? }

? ? ```


6. 構(gòu)造方法

構(gòu)造方法是一種特殊的方法,它的名字必須與它所在的類(lèi)的名字完全相同,并且沒(méi)有返回值,也不需要使用void進(jìn)行標(biāo)識(shí),在方法中不能用return語(yǔ)句返回一個(gè)值。構(gòu)造方法用于對(duì)對(duì)象中的所有成員變量進(jìn)行初始化,在創(chuàng)建對(duì)象時(shí)立即被調(diào)用。一個(gè)類(lèi)中可以有多個(gè)構(gòu)造方法。

? ? ```java

? ? package com.practice;

? ? public class Fruit {

? ? ? ? public String color;

? ? ? ? public Fruit(){? ? ? //定義構(gòu)造方法

? ? ? ? ? ? color="綠色";

? ? ? ? }

? ? ? ? public void harvest(){

? ? ? ? ? ? String color="紅色";

? ? ? ? ? ? System.out.println("水果是:"+color+"的!");

? ? ? ? ? ? System.out.println("水果已經(jīng)收獲~");

? ? ? ? ? ? System.out.println("水果原來(lái)是:"+this.color);

? ? ? ? }

? ? ? ? public static void main(String[] args) {

? ? ? ? ? ? Fruit fruit=new Fruit();

? ? ? ? ? ? fruit.harvest();

? ? ? ? }

? ? }

? ? ```


7. 對(duì)象

? ? ```java

? ? 類(lèi)名 對(duì)象名=new 類(lèi)構(gòu)造方法();

? ? ```


? ? 構(gòu)造方法:是類(lèi)創(chuàng)建對(duì)象時(shí)必須執(zhí)行的方法,用于構(gòu)造一個(gè)新的對(duì)象并初始化對(duì)象屬性;


8. 匿名對(duì)象


? ? ```java

? ? Calculate calculate = new Calculate();

? ? calculate.draw();


? ? new calculate.draw();

? ? ```


# 字符的集合-字符串

## 創(chuàng)建字符串

1. 產(chǎn)生String對(duì)象

? ? ```java

? ? String str=new String("java編程詞典");? //使用String類(lèi)的構(gòu)造方法創(chuàng)建字符串對(duì)象

? ? String str="java編程詞典";? //指定運(yùn)算

? ? ```? ?

2. 獲取字符串的長(zhǎng)度

? ? ```java

? ? str.length();

? ? ```


? ? ```java

? ? package com.practice;

? ? public class GetLength {

? ? ? ? public static void main(String[] args) {

? ? ? ? ? ? String name="一棵椰子樹(shù)";

? ? ? ? ? ? String address="一座孤島上";

? ? ? ? ? ? String message="在"+address+"有"+name;

? ? ? ? ? ? System.out.println("字符串"+name+"\t\t長(zhǎng)度為:"+name.length());? ? //字符串一棵椰子樹(shù) 長(zhǎng)度為:5

? ? ? ? ? ? System.out.println("字符串"+address+"\t\t長(zhǎng)度為:"+address.length());? //字符串一座孤島上 長(zhǎng)度為:5

? ? ? ? ? ? System.out.println("字符串"+message+"\t\t長(zhǎng)度為:"+message.length());? //字符串在一座孤島上有一棵椰子樹(shù) 長(zhǎng)度為:12

? ? ? ? }

? ? }

? ? ```? ?

3. 對(duì)字符串進(jìn)行截取

? ? ```java

? ? str.substring(int beginIndex)? //指定從某一索引處開(kāi)始截取字符串

? ? ```

? ? ```java

? ? String str="Hello World";

? ? String substr=str.substring(3);? ? //lo World

? ? ```

? ? ```java

? ? substring(int beginIndex,endIndex)? //返回從字符串某一索引位置開(kāi)始截取,至某一索引位置結(jié)束的子字符串

? ? ```

? ? ```java

? ? String str="Hello World";

? ? String substr=str.substring(0,7);? ? //Hello W 不包含endIndex這個(gè)索引位置的字符

? ? ```

4. 分割字符串

? ? ```java

? ? str.split(String sign)? //根據(jù)給定的分隔符對(duì)字符串進(jìn)行拆分

? ? sign:分割字符串的分隔符,也可以使用正則表達(dá)式

? ? ```

? ? ```java

? ? str.split(String sign,int limit) //根據(jù)給定的分隔符對(duì)字符串進(jìn)行拆分,并限定拆分的次數(shù)

? ? ```

? ? ```java

? ? package com.practice;

? ? public class SubString {

? ? ? ? public static void main(String[] args) {

? ? ? ? ? ? String str = "公司名稱(chēng):明日科技!公司所在城市:長(zhǎng)春市。公司電話(huà):********";

? ? ? ? ? ? String[] info = null;

? ? ? ? ? ? info = str.split("!|。");

? ? ? ? ? ? for (int i = 0; i < info.length; i++) {

? ? ? ? ? ? ? ? System.out.println(info[i]);

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? ```? ?

5. 去除字符串尾部空格

? ? ```java

? ? str.trim()?

? ? ```

6. 查找字符串

? ? ```java

? ? str.indexOf(ch) //用于獲取字符串中指定字符第一次出現(xiàn)處的索引,若字符沒(méi)有出現(xiàn)則返回-1

? ? ```

? ? ```java

? ? str.lastindexOf(ch) //用于獲取字符串中指定字符最后一次出現(xiàn)處的索引,若字符沒(méi)有出現(xiàn)則返回-1

? ? ```

? ? ```java

? ? str.indexOf(substr) //用于獲取字符串中指定字符串首次出現(xiàn)的索引位置,如果子字符串作為該字符串的子字符串出現(xiàn),則返回該子字符串的第一個(gè)字符的索引,如果沒(méi)有作為一個(gè)子字符串出現(xiàn)則返回-1

? ? ```

? ? ```java

? ? str.lastindexOf(substr) //用于獲取字符串中指定字符串最后一次出現(xiàn)的索引位置,如果子字符串作為該字符串的子字符串出現(xiàn)了一次或更多次,則返回最后一次出現(xiàn)該子字符串的第一個(gè)字符的索引,如果沒(méi)有作為一個(gè)子字符串出現(xiàn)則返回-1

? ? ```

? ? ```java

? ? indexOf(ch,startIndex) //根據(jù)指定的搜索起點(diǎn),搜索指定字符首次出現(xiàn)的位置

? ? ch:一個(gè)字符,unicode代碼點(diǎn)

? ? ```

? ? ```java

? ? int lastindexOf(ch,startIndex) //根據(jù)指定的搜索起點(diǎn),搜索指定字符最后一次出現(xiàn)的位置

? ? ch:一個(gè)字符,unicode代碼點(diǎn)

? ? ```


7. 比較字符串是否相等

? ? ```java

? ? boolean equals(String str)? ? ? //嚴(yán)格區(qū)分大小寫(xiě)

? ? boolean equalsIgnoreCase(otherString)? ? ? //不區(qū)分大小寫(xiě)

? ? ```


# 繼承與多態(tài)

## 父類(lèi)與子類(lèi)

1. 定義子類(lèi)

? ? ```java

? ? [修飾符]class 子類(lèi)名 extends 父類(lèi)名

? ? 修飾符:可選參數(shù),用于指定類(lèi)的訪(fǎng)問(wèn)權(quán)限,可選值為:public,abstract,final

? ? 子類(lèi)名:必須參數(shù),一般要求首字母大寫(xiě)

? ? ```

? ? ```java

? ? package com.practice;

? ? public class Car {

? ? ? ? public String color="黑色";

? ? ? ? public int speed=1;

? ? ? ? public int level;

? ? ? ? public void gear(int num){

? ? ? ? ? ? level=num;

? ? ? ? ? ? speed=10*level*level;

? ? ? ? }

? ? ? ? public void driver(){

? ? ? ? ? ? System.out.println("我是一輛"+color+"的轎車(chē)。");

? ? ? ? ? ? System.out.println("現(xiàn)在以時(shí)速"+speed+"公里每小時(shí)的速度形式中。");

? ? ? ? ? ? System.out.println("車(chē)檔位是"+level+"檔。");

? ? ? ? }

? ? }

? ? ```

? ? ```java

? ? package com.practice;

? ? public class SonCar extends Car {

? ? ? ? public static void main(String[] args){

? ? ? ? ? ? SonCar sonCar=new SonCar();

? ? ? ? ? ? sonCar.gear(5);

? ? ? ? ? ? sonCar.driver();

? ? ? ? }

? ? }

? ? ```

? ? ```

? ? --------------

? ? 我是一輛黑色的轎車(chē)。

? ? 現(xiàn)在以時(shí)速250公里每小時(shí)的速度形式中。

? ? 車(chē)檔位是5檔。

? ? --------------

? ? ```

2. 子類(lèi)覆蓋父類(lèi)

? ? ```

? ? 1. 方法的覆蓋也叫做方法的重寫(xiě),被覆蓋的方法必須和覆蓋方法具有相同的方法名稱(chēng)、參數(shù)以及返回值類(lèi)型;

? ? 2. 子類(lèi)不能覆蓋父類(lèi)中聲明為final或者static的方法;

? ? 3. 子類(lèi)必須覆蓋父類(lèi)中聲明為abstract的方法,或者子類(lèi)也將該方法聲明為abstract;

? ? 4. 子類(lèi)覆蓋父類(lèi)中的同名方法時(shí),子類(lèi)中方法的聲明也必須和覆蓋中被覆蓋的方法的聲明相同;

? ? 5. 子類(lèi)的訪(fǎng)問(wèn)權(quán)限必須要比父類(lèi)的訪(fǎng)問(wèn)權(quán)限大;

? ? ```

? ? ```java

? ? package com.practice;

? ? public class Person {

? ? ? ? public String name;

? ? ? ? public int age;

? ? ? ? public void getInfo(){

? ? ? ? ? ? System.out.println("姓名為:"+name);

? ? ? ? ? ? System.out.println("年齡為:"+age);

? ? ? ? }

? ? }

? ? ```

? ? ```java

? ? package com.practice;

? ? public class Worker extends Person{

? ? ? ? String company;

? ? ? ? public void getInfo(){

? ? ? ? ? ? System.out.println("姓名為:"+name);

? ? ? ? ? ? System.out.println("年齡為:"+age);

? ? ? ? ? ? System.out.println("公司為:"+company);

? ? ? ? }


? ? ? ? public static void main(String[] args){

? ? ? ? ? ? Worker worker=new Worker();

? ? ? ? ? ? worker.name="張三";

? ? ? ? ? ? worker.age=18;

? ? ? ? ? ? worker.company="科技公司";

? ? ? ? ? ? worker.getInfo();

? ? ? ? }

? ? }

? ? ```

3. super關(guān)鍵字

如果在子類(lèi)中想調(diào)用父類(lèi)中被覆蓋的方法,可以使用super關(guān)鍵字。

? ? * 使用super關(guān)鍵字操作父類(lèi)中被覆蓋的方法


? ? 如果要在子類(lèi)中使用被父類(lèi)隱藏的成員變量或方法,就可以使用super關(guān)鍵字。

? ? 例如訪(fǎng)問(wèn)父類(lèi)的成員變量x:

? ? ```java

? ? super.x

? ? ```

例如訪(fǎng)問(wèn)父類(lèi)中的方法play(),代碼為:

? ? ```java

? ? super.play()

? ? ```

? ? ```java

? ? package com.practice;

? ? public class Worker extends Person{

? ? ? ? String company;

? ? ? ? public void getInfo(){

? ? ? ? ? ? super.getInfo();

? ? ? ? ? ? System.out.println("公司為:"+company);

? ? ? ? }


? ? ? ? public static void main(String[] args){

? ? ? ? ? ? Worker worker=new Worker();

? ? ? ? ? ? worker.name="張三";

? ? ? ? ? ? worker.age=18;

? ? ? ? ? ? worker.company="科技公司";

? ? ? ? ? ? worker.getInfo();

? ? ? ? }

? ? }

? ? ```

? ? * 使用super關(guān)鍵字操作父類(lèi)中被覆蓋的方法調(diào)用父類(lèi)的構(gòu)造方法


? ? 子類(lèi)并不繼承父類(lèi)的構(gòu)造方法。如果子類(lèi)要使用父類(lèi)的構(gòu)造方法,需要使用super關(guān)鍵字,并且一定要在子類(lèi)的構(gòu)造方法中使用。



? ? ```java

? ? super([構(gòu)造參數(shù)列表])

? ? ```?

4. 方法重載

在一個(gè)類(lèi)中,出現(xiàn)多個(gè)相同名稱(chēng)的方法,但是參數(shù)類(lèi)型與參數(shù)個(gè)數(shù)不相同。

# 接口與抽象類(lèi)

## 接口

1. 接口定義

? ? ```java

? ? [修飾符]interface 接口名 [extends 父接口名列表]{

? ? [public][static][final] 常量;

? ? [public][abstract]方法;

? ? }

? ? ```

? ? ```java

? ? public interface Calculate{

? ? ? ? final float PI=3.14159f;

? ? ? ? float getArea(float r);

? ? ? ? float gerCircumference(float r);

? ? }

? ? ```?

2. 實(shí)現(xiàn)接口

在實(shí)現(xiàn)接口時(shí),一次則可以實(shí)現(xiàn)多個(gè)接口,每個(gè)接口間使用逗號(hào)分隔。


? ? ```java

? ? [修飾符] class <類(lèi)名> [extends 父類(lèi)名] [implemennts 接口列表]{

? ? }

? ? ```

? ? ```java

? ? package com.practice;

? ? public interface Calculate1 {

? ? ? ? final float PI=3.14159f;

? ? ? ? float getArea(float r);

? ? ? ? float getCircumference(float r);

? ? }

? ? ```

? ? ```java

? ? package com.practice;

? ? public class Round implements Calculate1 {

? ? ? ? @Override

? ? ? ? public float getArea(float r) {

? ? ? ? ? ? float area = PI * r * r;

? ? ? ? ? ? return area;

? ? ? ? }

? ? ? ? @Override

? ? ? ? public float getCircumference(float r){

? ? ? ? ? ? float circumference=2*PI*r;

? ? ? ? ? ? return circumference;

? ? ? ? }


? ? ? ? public static void main(String[] args){

? ? ? ? ? ? Round round=new Round();

? ? ? ? ? ? float r=3f;

? ? ? ? ? ? float area=round.getArea(r);

? ? ? ? ? ? float circumference=round.getCircumference(r);


? ? ? ? ? ? System.out.println("半徑為:"+r+"的圓的面積為:"+area);

? ? ? ? ? ? System.out.println("半徑為:"+r+"的圓的周長(zhǎng)為:"+circumference);

? ? ? ? }

? ? }

? ? ```?

3. 定義抽象類(lèi)

抽象類(lèi)只聲明方法的存在,而不去具體實(shí)現(xiàn)它的類(lèi)。

抽象類(lèi)不能被實(shí)例化,不能使用new關(guān)鍵字來(lái)創(chuàng)建對(duì)象。

使用抽象類(lèi)的好處在于,當(dāng)有的方法在父類(lèi)中不想實(shí)現(xiàn)時(shí),可以不用實(shí)現(xiàn)。

? ? ```java

? ? abstract class 類(lèi)名{

? ? ? ? 類(lèi)體

? ? }

? ? ```

? ? ```java

? ? abstract class Fruit{

? ? ? ? public String color;

? ? ? ? public Fruit(){

? ? ? ? ? ? color="綠色";

? ? ? ? }

? ? }

? ? ```

4. 解析抽象方法

在一個(gè)抽象類(lèi)中,可以定義一個(gè)或多個(gè)抽象方法;

定義抽象方法需要在方法的聲明處使用關(guān)鍵字abstract.

? ? ```java

? ? abstract <方法返回值類(lèi)型> 方法名(參數(shù)列表);

? ? ```

? ? ```java

? ? public abstract void harvest();

? ? ```

? ? ```java

? ? package com.practice;

? ? abstract class Fruit1 {

? ? ? ? public String color;


? ? ? ? public Fruit1() {

? ? ? ? ? ? color = "綠色";

? ? ? ? }

? ? ? ? public abstract void harvest();

? ? }

? ? ```

? ? ```java

? ? package com.practice;

? ? public abstract class Apple extends Fruit1{

? ? ? ? public String color;

? ? ? ? public Apple(){

? ? ? ? ? ? color="綠色";

? ? ? ? }

? ? ? ? public abstract void harvest();

? ? }

? ? ```

? ? ```java

? ? package com.practice;

? ? public class Pear extends Fruit1{

? ? ? ? public void harvest(){

? ? ? ? ? ? System.out.println("大白梨成熟啦!");

? ? ? ? }

? ? }

? ? ```

? ? ```java

? ? package com.practice;

? ? public class Farm {

? ? ? ? public static void main(String[] args) {

? ? ? ? ? ? Apple apple= new Apple() {

? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? public void harvest() {

? ? ? ? ? ? ? ? ? ? System.out.println("大蘋(píng)果成熟啦!");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? };

? ? ? ? ? ? apple.harvest();

? ? ? ? ? ? Pear pear=new Pear();

? ? ? ? ? ? pear.harvest();

? ? ? ? }

? ? }

? ? ```

5. 創(chuàng)建內(nèi)部類(lèi)

一個(gè)類(lèi)定義在另一個(gè)類(lèi)的內(nèi)部,那么就稱(chēng)之為內(nèi)部類(lèi),也叫做嵌套類(lèi)。

在類(lèi)中直接定義的內(nèi)部類(lèi)的使用范圍,僅限于這個(gè)類(lèi)的內(nèi)部。

例如,在類(lèi)A中定義了類(lèi)B,那么類(lèi)B為類(lèi)A所知,但卻不被A的外部所知。

在內(nèi)部類(lèi)中可以引用它所在的外部類(lèi)的所有變量和方法。

內(nèi)部類(lèi)可以使用private或protected來(lái)修飾。

? ? ```java

? ? package com.practice;

? ? public class Outer {

? ? ? ? private String strOut = "我是外部類(lèi)中的變量";


? ? ? ? private void test() {

? ? ? ? ? ? Inner inner = new Inner();

? ? ? ? ? ? inner.transfer();

? ? ? ? }


? ? ? ? class Inner {

? ? ? ? ? ? void transfer() {

? ? ? ? ? ? ? ? System.out.printf("在內(nèi)部類(lèi)中調(diào)用外部類(lèi)中變量: %s", strOut);

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? public static void main(String[] args) {

? ? ? ? ? ? Outer outer = new Outer();

? ? ? ? ? ? outer.test();

? ? ? ? }

? ? }

? ? ```


# 集合

## collection集合

1. Collection集合

? ? collection集合又稱(chēng)為容器,他們與數(shù)組不同,數(shù)組的長(zhǎng)度是固定的,集合的長(zhǎng)度是可變的,數(shù)組用來(lái)存放基本類(lèi)型的數(shù)據(jù),集合用來(lái)存放類(lèi)對(duì)象的引用。

2. collection接口

? ? collection接口是List接口和Set接口的父接口,通常情況下不被直接使用。

3. collection接口中的常用方法

? ? * add()方法? ? ? 向指定的集合中添加對(duì)象。

? ? ? ? ```java

? ? ? ? package com.practice;

? ? ? ? import java.util.ArrayList;

? ? ? ? import java.util.Collection;

? ? ? ? import java.util.Iterator;


? ? ? ? public class Append {

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? Collection list=new ArrayList();

? ? ? ? ? ? ? ? list.add("apple");

? ? ? ? ? ? ? ? list.add("pear");

? ? ? ? ? ? ? ? list.add("apple");

? ? ? ? ? ? ? ? Iterator it=list.iterator();

? ? ? ? ? ? ? ? System.out.println("集合中的元素有:");


? ? ? ? ? ? ? ? while (it.hasNext()){

? ? ? ? ? ? ? ? ? ? String str=(String)it.next();

? ? ? ? ? ? ? ? ? ? System.out.println(str);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? ```


? ? * addAll()方法? ? 將指定集合中的所有對(duì)象都添加到集合中,如果對(duì)該集合進(jìn)行了泛化,則要求指定集合中的所有對(duì)象都符合泛化類(lèi)型,否則編譯程序時(shí)將拋出異常。

? ? ? ? ```java

? ? ? ? package com.practice;

? ? ? ? import java.util.ArrayList;

? ? ? ? import java.util.Collection;

? ? ? ? import java.util.Iterator;


? ? ? ? public class AddGather {

? ? ? ? ? ? public static void main(String[] args){

? ? ? ? ? ? ? ? String apple="Apple";

? ? ? ? ? ? ? ? String pear="Pear";

? ? ? ? ? ? ? ? String orange="Orange";

? ? ? ? ? ? ? ? Collection<String> list =new ArrayList<String>();

? ? ? ? ? ? ? ? list.add("apple");

? ? ? ? ? ? ? ? list.add("pear");

? ? ? ? ? ? ? ? list.add("orange");

? ? ? ? ? ? ? ? Collection<String> list2 =new ArrayList<String>();

? ? ? ? ? ? ? ? list2.addAll(list);

? ? ? ? ? ? ? ? Iterator<String> it=list2.iterator();


? ? ? ? ? ? ? ? System.out.println("集合中的對(duì)象為:");

? ? ? ? ? ? ? ? while(it.hasNext()){

? ? ? ? ? ? ? ? ? ? String str=it.next();


? ? ? ? ? ? ? ? ? ? System.out.println(str);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? ```? ?

4. iterator() 方法? ? 在指定的集合對(duì)象中創(chuàng)建迭代器,返回在此Collection的元素上進(jìn)行迭代的迭代器。當(dāng)獲取迭代器后,通過(guò)迭代器Iterator對(duì)象的hasNext()方法,判斷集合中是有仍有元素可以迭代,如果有則返回true,否則返回false。hasNext()方法通常被用在循環(huán)語(yǔ)句中,作為判斷循環(huán)的條件。

5. removeAll()方法? ? 用來(lái)從該集合中移除同時(shí)包含在指定參數(shù)集合中的對(duì)象。如果集合中包含符合條件的對(duì)象,則返回true,否則返回false。

? ? ```java

? ? package com.practice;

? ? import java.util.ArrayList;

? ? import java.util.Collection;

? ? import java.util.Iterator;


? ? public class RemoveCollection {

? ? ? ? public static void main(String[] args){

? ? ? ? ? ? Collection <String>? list=new ArrayList<String>();

? ? ? ? ? ? list.add("Java編程詞典");

? ? ? ? ? ? list.add("C++編程詞典");

? ? ? ? ? ? list.add("VB編程詞典");

? ? ? ? ? ? list.add("C#編程詞典");

? ? ? ? ? ? list.add(".net編程詞典");


? ? ? ? ? ? Iterator<String> it=list.iterator();

? ? ? ? ? ? System.out.println("集合中原有的對(duì)象為:");


? ? ? ? ? ? while(it.hasNext()){

? ? ? ? ? ? ? ? String obj=(String)it.next();

? ? ? ? ? ? ? ? System.out.println(obj);

? ? ? ? ? ? }


? ? ? ? ? ? Collection <String> list2=new ArrayList<String>();

? ? ? ? ? ? list2.add(".net編程詞典");

? ? ? ? ? ? list2.add("C#編程詞典");

? ? ? ? ? ? list.removeAll(list2);

? ? ? ? ? ? Iterator<String> it2=list.iterator();

? ? ? ? ? ? System.out.println("移除指定對(duì)象后集合中的對(duì)象為:");


? ? ? ? ? ? while(it2.hasNext()){

? ? ? ? ? ? ? ? String obj=(String)it2.next();

? ? ? ? ? ? ? ? System.out.println(obj);

? ? ? ? ? ? }


? ? ? ? }

? ? }

? ? ```

? ? ```

? ? --------------------

? ? 集合中原有的對(duì)象為:

? ? Java編程詞典

? ? C++編程詞典

? ? VB編程詞典

? ? C#編程詞典

? ? .net編程詞典

? ? 移除指定對(duì)象后集合中的對(duì)象為:

? ? Java編程詞典

? ? C++編程詞典

? ? VB編程詞典

? ? --------------------

? ? ```

## List集合

List集合包括List接口與List接口的所有實(shí)現(xiàn)類(lèi)。List接口繼承Collection接口,因此具有該接口的所有方法。List集合中的對(duì)象允許重復(fù)。Set集合中的對(duì)象沒(méi)有順序。Map集合是按照Key-value形式進(jìn)行存儲(chǔ)的。

1. List接口

List集合為列表類(lèi)型,以線(xiàn)性方式存儲(chǔ)對(duì)象,可以通過(guò)對(duì)象的索引操作對(duì)象。因此該接口中包含一些與索引相關(guān)的方法。

2. ArrayList類(lèi)

ArrayList類(lèi)實(shí)現(xiàn)了List接口,由ArrayList類(lèi)實(shí)現(xiàn)的List集合采用數(shù)組結(jié)構(gòu)保存對(duì)象。

3. LinkedList類(lèi)

LinkedList類(lèi)實(shí)現(xiàn)了List接口,用LinkedList類(lèi)實(shí)現(xiàn)的List集合采用鏈表結(jié)構(gòu)保存對(duì)象。

## Set集合

Set集合為集類(lèi)型,集時(shí)最簡(jiǎn)單的一種集合,存放于集中的對(duì)象不按特定方式排序,只是簡(jiǎn)單地把對(duì)象加入集合中,類(lèi)似于向口袋中放東西。不能存放重復(fù)對(duì)象。

## Map集合

Map沒(méi)有繼承Collection接口,其提供的是key到value的映射。Map中不能包含相同的key值,每個(gè)key只能映射一個(gè)value。

1. Map接口

Map接口提供了將鍵映射到值的對(duì)象,集合中不能包含重復(fù)的鍵。

2. HashMap類(lèi)

HashMap類(lèi)實(shí)現(xiàn)了Map接口,由HashMap類(lèi)實(shí)現(xiàn)的Map集合,允許以null作為鍵對(duì)象,但是因?yàn)榻?duì)象不可以重復(fù),所以這樣的鍵對(duì)象只能有一個(gè)。

3. TreeMap類(lèi)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容