2007年11月9日 星期五

Head First Java 讀後整理(7) - Numbers Matter

1、當 method 的行為皆與 instance variable 無關時,僅對 argument 有執行動作,就設定為「static」吧
static method 的使用並不需要有實際的 object 存在,如果 method 僅對 argument 有效用,設定為 static 反而可以省下 heap 寶貴的空間,例如:Math class 中的 abs() method (取絕對值)

2、要限制 non-abstract class 被初始化,可將所有 constructor 的存取權限設定為 private

3、static method 中不能使用 non-static variable
因為 static method 是在無關特定 class 的 instance 下執行,不應該參考到任何 instance variable


4、static method 中不能呼叫其他 non-static method

5、static variable 的值,對所有的 instance 來說都是相同的,也是共用的

6、static variable 會在該 class 的任何 object 建構出之前,以及 static method 執行之前就初始化完成

7、若沒有給 static variable 初始值,它就會自動被設定成預設值
long、int、shot => 0
double、float => 0.0
boolean => false
object reference => null

8、被標示為「final」的 static variable 代表它一旦被初始化後就不會更動,一般會稱為 Constant(常數),且命名方式都是大寫並以底線分隔

9、static final variable 必須初始化,否則 compiler 會顯示錯誤訊息

10、「final」不只可用在 static variable 上
final 的 variable 代表不能改變它的值
final 的 method 代表不能被 override
final 的 class 代表不能被 extend

11、若 class 已被標示為 final,其 method 就不需要標示為 final (因為那是多餘的)

12、Java 5.0 版開始加入「autoboxing」功能,能將 primitive 自動轉換為包裝過的 object
//Java 5.0 之前,沒有 Autoboxing 的功能
public void doNumsOldWay() {
ArrayList listOfNumbers = new ArrayList();
//必須以 object 的形式加入
listOfNumbers.add(new Integer(3));
//要針對 return type 進行轉換
Integer one = (Integer)listOfNumbers.get(0);
//最後還得透過 Integer class 所提供的 method 取得原值
int intOne = one.intValue();
}

//In Java 5.0,已經提供 Autoboxing 的功能
public voif doNumsNewWay() {
//建構 Integer 型別的 ArrayList
ArrayList<Integer> listOfNumbers = new ArrayList<Integer>();
//直接加!
listOfNumbers.add(3);
//compiler 會自動解開 Integer 的包裝,因此可以直接指派給 int
int num = listOfNumbers.get(0);
}

13、使用 Autoboxing 還是必須注意 object 與 primitive 之間轉換時的差異
public class TestBox {
Integer i; // object reference => 預設值為 null
int j; // primitive => 預設值為 null

public static void main(String[] args) {
TestBox t = new TestBox();
t.go();
}

public void go() {
j = i; // i 只是 object reference,預設值為 null
//以下兩行會發生錯誤,因為 i 與 j 都是 null
System.out.println(j);
System.out.println(i);

//若是換成下列寫法
i = j; // j 是 primitive,預設值為 0
//以下兩行都會正常處理而顯示 0
System.out.println(j);
System.out.println(i);
}
}

14、數字的格式化語法 => %[argument number][flags][width][.precision]type
public class NumberFormat {
public static void main(String args[]) {
System.out.println(String.format("%d", 42)); //42
System.out.println(String.format("%.3f", 42.000)); //42.000
System.out.println(String.format("%,.2f", 45674.9876)); //45,678.99
System.out.println(String.format("%,d", 10000000)); //10,000,000
System.out.println(String.format("%x", 42)); //2a
System.out.println(String.format("%c", 42)); //*

//variable argument list
//The rank is 234,567 out of 123,456.12
System.out.println(String.format("The rank is %,d out of %,.2f", 234567, 123456.12345));
}
}

15、日期的格式化
import java.util.*;
public class DateFormat {
public static void main(String args[]) {
Date today = new Date();

//完整的日期與時間:%tc
System.out.println(String.format("%tc", today)); //星期五 十一月 09 11:51:37 CST 2007
//只有時間:%tr
System.out.println(String.format("%tr", today)); //11:51:37 上午
//週、月、日:%tA %tB %td
System.out.println(String.format("%tA %tB %td", today, today, today)); //星期五 十一月 09
//同上,但不用重複給 argument
System.out.println(String.format("%tA %<tB %<td", today, today, today)); //星期五 十一月 09
}
}

16、處理日期,可使用強大的 Calendar
由於 Calendar 是 abstract class,因此無法將 Calendar 實體化,因此要改用下面方式取得 Calendar 物件:

Calendar cal = Calendar.getInstance();

透過使用 getInstance() 這個 static method,就可以取得具體 subclass 的 instance(Calendar 的 polymorphism 變化版本),然而根據 polymorphism 的機制規定,此 instance 會提供 Calendar 該提供的所有 method。





沒有留言:

張貼留言