20145227 《Java程序設計》第3周學習總結
教材學習內容總結
第四章 認識對象
4.1 類與對象
1、定義類:生活中描述事物無非就是描述事物的屬性和行為。如:人有身高,體重等屬性,有說話,打球等行為。Java中用類class來描述事物也是如此。
- 屬性:對應類中的成員變量。
- 行為:對應類中的成員函數。
- 定義類其實在定義類中的成員(成員變量和成員函數)。
- 代碼如下:
class Clothes
{
String color;
char size;
}
public class Field
{
public static void main(String[] args)
{
Clothes sun = new Clothes();
Clothes spring = new Clothes();
sun.color = "red";
sun.size = 'S';
spring.color = "green";
spring.size = 'M';
System.out.printf("sun(%s,%c)%n",sun.color,sun.size);
System.out.printf("spring(%s,%c)%n",spring.color,spring.size);
}
}
-
結果截圖:

-
構造函數中,由於參數與數據類型重名,需使用this將參數值指定給參數。定義構造函數代碼如下。
class Clothes2
{
String color;
char size;
Clothes2(String color, char size)
{
this.color=color;
this.size=size;
}
}
public class Field2
{
public static void main(String[] args)
{
Clothes2 sun = new Clothes2("red",'S');
Clothes2 spring = new Clothes2("green",'M');
System.out.printf("sun(%s,%c)%n",sun.color,sun.size);
System.out.printf("spring(%s,%c)%n",spring.color,spring.size);
}
}
- 結果截圖:

2、使用標准類:
- 使用java.util.Scanner
- 代碼如下:
import java.util.Scanner;
public class Guess
{
public static void main(String[] args)
{
Scanner scanner = new Scanner (System.in);
int number = (int) (Math.random() * 10);
int guess;
do
{
System.out.printf("GUESS A NUMBER:");
guess = scanner.nextInt();
}
while(guess != number);
System.out.println("YOU ARE RIGHT!");
}
}
- 結果截圖:

- 使用java.math.BigDecimal
- 代碼如下:
import java.math.BigDecimal;
public class DecimalDemo
{
public static void main(String[] args)
{
BigDecimal operand1 = new BigDecimal ("1.0");
BigDecimal operand2 = new BigDecimal ("0.8");
BigDecimal result = operand1.subtract(operand2);
System.out.println(result);
}
}
- 結果截圖:

- 用BigDecimal比較相等代碼如下:
import java.math.BigDecimal;
public class DecimalDemo2
{
public static void main(String[] args)
{
BigDecimal o1 = new BigDecimal ("0.1");
BigDecimal o2 = new BigDecimal ("0.1");
BigDecimal o3 = new BigDecimal ("0.1");
BigDecimal result = new BigDecimal("0.3");
if(o1.add(o2).add(o3).equals(result))
{
System.out.println("等於0.3");
}
else
{
System.out.println("不等於0.3");
}
}
}
- 結果截圖:

4.2 基本類型打包器
1、打包基本類型:基本類型long、int、double、float、boolean等,在J2SE5.0之前必須親自使用Long、Integer、Double、Float、Boolean等類打包為對象,才能當做對象來操作。
- 代碼如下:
public class IntegerDemo
{
public static void main(String[] args)
{
int data1 = 10;
int data2 = 20;
Integer wrapper1 = new Integer(data1); //打包基本類型
Integer wrapper2 = new Integer(data2);
System.out.println(data1/3); //基本類型運算
System.out.println(wrapper1.doubleValue()/3); //操作打包器方法
System.out.println(wrapper1.compareTo(w2));
}
}
- 結果截圖:

2、自動裝箱、拆箱:
- 自動裝箱運用的方法可以如下:
int i=10;
Integer wrapper=i;
- 可以用Number類來自動裝箱:
Number number = 3.14f;
(3.14f會被先自動裝箱為Float,然后再指定給number。)
- 自動拆箱:即自動取出打包器中的基本形態信息。例如:
Integer wrapper = 10; //自動裝箱
int foo = wrapper; //自動拆箱
(wrapper會參考至Integer,若被指定給int型的變量foo,則會自動取得打包的int類型再指定給foo。)
4.3 數組對象
1.數組基礎
- 聲明數組來儲存XY坐標位置要放的值代碼如下:
public class XY
{
public static void main(String[] args)
{
int[][] cords={
{1,2,3},
{4,5,6}
};
for(int[] row : cords)
{
for(int value : row)
{
System.out.printf("%2d",value);
}
System.out.println();
}
}
}
- 結果截圖:

2.操作數組對象:
- 將每個學生成績默認為60分起代碼如下:
import java.util.Arrays;
public class Score2
{
public static void main(String[] args)
{
int[] scores = new int[10];
for(int score : scores)
{
System.out.printf("%2d",score);
}
System.out.println();
Arrays.fill(scores,60);
for(int score : scores)
{
System.out.printf("%3d",score);
}
}
}
- 結果截圖:

3.數組復制:
- 代碼如下:
import java.util.Arrays;
public class Copy
{
public static void main(String[] args)
{
int[] scores1 = {88,81,74,68,78,76,77,85,95,93};
int[] scores2 = Arrays.copyOf(scores1,scores1.length);
for(int score : scores2)
{
System.out.printf("%3d",score);
}
System.out.println();
scores2[0] = 99;
for(int score : scores1)
{
System.out.printf("%3d",score);
}
}
}
- 結果截圖:

- 深層復制代碼如下:
class Clothes2
{
String color;
char size;
Clothes2(String color, char size)
{
this.color=color;
this.size=size;
}
}
public class DeepCopy
{
public static void main(String[] args)
{
Clothes2[] c1 = {new Clothes2("red",'S'),new Clothes2("green",'M')};
Clothes2[] c2 = new Clothes2[c1.length];
for(int i = 0; i < c1.length; i++)
{
Clothes2 c = new Clothes2(c1[i].color, c1[i].size);
c2[i] = c;
}
c1[0].color = "yellow";
System.out.println(c2[0].color);
}
}
- 結果截圖:

4.4 字符串對象
1.字符串基礎:由字符組成的文字符號稱為字符串。
- 用戶輸入整數,再輸入0后會計算所有整數總和代碼如下:
import java.util.Scanner;
public class Sum
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
long sum = 0;
long number = 0;
do
{
System.out.print("輸入數字:");
number = Long.parseLong(scanner.nextLine());
sum += number;
}
while(number != 0);
System.out.println("總和為:"+sum);
}
}
- 結果截圖:

2.字符串特性:
-
字符串常量與字符串池:用“”寫下的字符串稱為字符串常量。
-
不可變動字符串:在java中,字符串對象一旦建立,就無法更改對象中任何內容,對象上沒有任何方法可以更改字符串內容。可以使用 + 連接字符串,改用StringBuilder來改善。
-
代碼如下:
public class One
{
public static void main(String[] args)
{
StringBuilder builder = new StringBuilder();
for(int i = 1; i < 100; i++)
{
builder.append(i).append('+');
}
System.out.println(builder.append(100).toString());
}
}
- 結果截圖:

4.5 查詢API文檔
- 學習Java要學會使用Java API,在Windows下一定要下載CHM格式的Java API,這個版本具有檢索功能,使用非常方便。
第五章 對象封裝
5.1 何謂封裝
- 封裝(Encapsulation)實際上是使用方法(Method)將類的數據隱藏起來,控制用戶對類的修改和訪問數據的程度,隱藏對象細節,將對象當作黑箱進行操作。
1.通過構造方法(構造函數)封裝初始化流程 - 代碼如下:
class Cashcard{
String number;
int balance;
int bonus;
CashCard(String number,int balance,int bonus){
this.number = number;
this.balance = balance;
this.bonus = bonus;
}
}
public class CardApp
{
public static void main(String[] args)
{
CashCard[] cards={
new CashCard("A001",500,0),
new CashCard("A002",300,0),
new CashCard("A003",1000,1),
new CashCard("A004",2000,2),
new CashCard("A005",3000,3)
};
for(CashCard card : cards)
{
System.out.printf("(%s,%d,%d)%n",card.number,card.balance,card.bonus);
}
}
}
2.通過成員方法(函數)封裝操作
- 代碼如下:
import java.util.Scanner;
public class CashApp
{
public static void main(String[] args)
{
CashCard[] cards={
new CashCard("A001",500,0),
new CashCard("A002",300,0),
new CashCard("A003",1000,1)
};
Scanner scanner = new Scanner(System.in);
for(CashCard card : cards)
{
System.out.printf("為(%s,%d,%d)儲值:",card.number,card.balance,card.bonus);
card.store(scanner.nextInt());
System.out.printf("明細(%s,%d,%d)%n",card.number,card.balance,card.bonus);
}
}
}
3.通過成員變量封裝數據
- private關鍵字:是一個權限修飾符。用於修飾成員(成員變量和成員函數)。被私有化的成員只在本類中有效。
- 功能:將成員變量私有化,對外提供對應的set ,get 方法對其進行訪問。可提高對數據訪問的安全性。
5.2 類語法細節
1.public權限修飾
- 如果沒有聲明權限修飾的成員,只有在相同包的類程序代碼下才可以直接存取,也就是“包范圍權限”。如果想在其他包的類程序代碼中存取某包的類或對象成員,則該類或對象成員必須是公開成員,在java中要使用public加以聲明。
2.關於構造函數
- 特點:(1) 函數名與類名相同 ;(2)不用定義返回值類型; (3)不可以寫return語句。
- 作用: 給對象進行初始化。
- 注意:(1) 默認構造函數的特點。(2) 多個構造函數是以重載的形式存在的。
3.方法重載
- 代碼如下:
class Some
{
void someMethod(int i)
{
System.out.println("int 版本被調用");
}
void someMethod(Integer integer)
{
System.out.println("Integer 版本被調用");
}
}
public class Overload
{
public static void main(String[] args)
{
Some s = new Some();
s.someMethod(1);
}
}
- 結果截圖:

- 如果想調用參數為Integer版本的方法則代碼如下:
class Some
{
void someMethod(int i)
{
System.out.println("int 版本被調用");
}
void someMethod(Integer integer)
{
System.out.println("Integer 版本被調用");
}
}
public class Overload2
{
public static void main(String[] args)
{
Some s = new Some();
s.someMethod(new Integer(1));
}
}
- 結果截圖:

3.this關鍵字
- 特點:this代表其所在函數所屬對象的引用。 換言之:this代表類對象的引用。
- 什么時候使用this關鍵字呢? 當在函數內需要用到調用該函數的對象時,就用this。
- 代碼如下:
class Other{
{
System.out.println("對象初始區塊");
}
Other()
{
System.out.println("Other() 構造函數");
}
Other(int o )
{
this();
System.out.println("Other(int o ) 構造函數");
}
}
public class ObjectInitialBlock
{
public static void main(String[] args)
{
new Other(1);
}
}
- 結果截圖:

4.static關鍵字
- 用於修飾成員(成員變量和成員函數)
- 被修飾后的成員具備以下特點:(1)隨着類的加載而加載;(2)優先於對象存在;(3)被所有對象所共享;(4)可以直接被類名調用;
- 使用注意:(1)靜態方法只能訪問靜態成員;(2)靜態方法中不可以寫this,super關鍵字;(3)主函數是靜態的。
- import static語法代碼如下:
- 代碼如下:
import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;
public class ImportStatic
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(in);
out.print("請輸入姓名:");
out.printf("%s 你好!%n",scanner.nextLine());
}
}
- 結果截圖:

- 代碼托管截圖

教材學習中的問題和解決過程
這次的內容相對前兩周來說增加了很多,教材有些地方也晦澀難懂。不過通過看視頻以及問問題,很多地方也明白了,不過還有很多地方都需要多看、多練習。
代碼調試中的問題和解決過程
通過構造方法(構造函數)封裝初始化流程時,教材上說構造函數是與類名稱同名的方法,不用聲明返回類型,但是編譯時一直顯示“方法聲明無效,需要返回類型”。如下圖所示:

解決途徑:尚未解決。
其他(感悟、思考等,可選)
學習java已經三個星期了,這周學習了java第四章第五章的內容。這周編寫的代碼比上周多很多,感覺也更加熟練了。而且這周學會了托管代碼。一開始托管代碼不成功,后面根據同學博客上的步驟一步一步去做,最后終於成功托管了代碼。過程很繁瑣,但所幸努力有了回報,現在已經會托管代碼了。
學習進度條
| 代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
|---|---|---|---|---|
| 目標 | 5000行 | 30篇 | 400小時 | |
| 第一周 | 200/200 | 2/2 | 20/20 | |
| 第二周 | 200/400 | 1/3 | 20/40 | |
| 第三周 | 500/900 | 1/4 | 30/70 |
