Java 是一種十分基礎並常用的編程語言。 它的歷史非常悠久,在許多新語言的研究和開發中擔任著重要的角色, 但亦因此 Java 比其他常用語言更為繁瑣和嚴謹。 不過由於 Java 穩定的表現, 豐富的文檔和輕鬆搬移項目的靈活特性,至今仍然有不少從事資訊科技的公司採用。


學習目標

  • 掌握Java Prograqmming的基礎
  • 能夠創建屬於自己的Project
  • 學習一些Java的進階指令

章節1 - 如何開始寫Java

市面上有不少支援 Java Development 的軟件
假如你是完全沒接觸過 Java 的同學, 建議先下載 Java Development Kit (JDK) 和相應的開發軟件
如 Eclipse, Visual Studio, Netbeans ...
本文將會專注於使用 Apache Netbeans, Java with Ants 的開發上

首先介紹 Java 的基礎文本範例
假設你在 Package example 中創建了一個 HelloWorld.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 必須指定附屬的 Package
package example;

// 可添加所需的 Libraries, 如
// import java.util.*;

// 每個 Java 檔案都必須有一個跟[檔案名稱]一樣的 Public Class
// 注意此處 Class 的名稱是 Case Sensitive,必須完全一樣
public class HelloWorld {

// 主要運行的部分
public static void main(String[] args) {
// 你的代碼
System.out.print("Hello World");
}

}

main 是主要執行的部分, 想要創建或呼召任何定義功能都必須寫在 main 裡
執行後將會 Output

1
2
Hello World

注意事項

Java 十分重視句式結尾,千萬不要忘記在最後加上分號 ; (儘管不少IDE會自動提醒你)


如何在不使用開發軟件的前提下,單獨運行 Java 檔案

  1. 首先你的裝置上必須安裝了 JDK
  2. 手上持有 Java 檔案,例: [檔案名稱].java
  3. 打開命令提示元 CMD 並輸入 cd [檔案位置] 將目標位置設定在檔案文件夾
  4. 輸入 javac [檔案名稱].java 以創建執行 Java 用的檔案 [檔案名稱].class
  5. 最後在命令提示元中輸入 java [檔案名稱].java 即可

如何單獨運行 Java 檔案並附帶參數 Parameter

  1. 首先依照上述的指示做一遍
  2. 在最後一步的指令後方添加任意Parameters
    java [檔案名稱].java <parameter1> <parameter2> <...>
    所有Parameters將會儲進一個Array並傳遞至main裡面的String[] args
    你便可以在main裡根據Array的內容進行其餘的指令, 如
    1
    2
    3
    4
    5
    6
    7
    public static void main(String[] args) {
    // 假設傳遞的 Parameter 是整數
    // 求 Print 出第一個 Parameter 加 第二個 Parameter 的結果
    // 注意此處 args 為 String, 必須轉化後才能進行數字上的計算
    System.out.println(Integer.parseInt(args[0]) + Integer.parseInt(args[1]));
    }

章節二 - Objects Oriented Programming (OOP)

Java 開發的核心基本圍繞在 Object, 而 Object 的原型就是 Class

甚麼是 Class

簡單來說 Class 是一個包含虛化定義的 Template, 在實體化後就變成 Object
舉個例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Grape {
float price = 10; // 價錢 $10
int quantity = 3; // 有3束葡萄
}

public class HelloWorld {

public static void main(String[] args) {
// 實體化 Grape Class
Grape grapeA = new Grape();
// Class 可以被重複創建並賦予不同的定義, 後續會提到
Grape grapeB = new Grape();
}

}

Class type 種類

不同的種類會導致不同的存取權限
分類不限於 Class ,更能用在 Variable 和 Method 上

  • Public (最大權限)
    Project內任何檔案都能存取
  • Protected
    同一 Class, Package, 其他 Package 的 Subclass 能存取
  • Default
    僅限同一 Class, Package 能存取
  • Private (最小權限)
    僅限同一 Class 能存取

例子

1
2
3
4
5
6
7
8
9
10
11
protected class Grape {
// Variable
float price = 10; // Default
private int quantity = 3;

// Method
public int purchase(){
// ...
};
}

甚麼是 Method

Method主要用於定義功能,執行範圍內的指令且能選擇是否返回數值
在絕大部分語言中稱作 Function, 而在 Java 裡叫 Method

常見例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Grape {
float price = 10;
int quantity = 3;

// 執行 Method 後返回 float 數值
// 俗稱 Getter, 僅讀取 variables
float getPrice() {
return price;
}

// void 代表執行 Method 後無需返回任何數值
// 俗稱 Setter, 僅更新 variables
void setPrice(float newPrice) {
price = newPrice;
}

// 也是稱作 Method
public static void main(String[] args) {
// 主代碼
}
}

每個 Class裡都有的 Constructor

Constructor是會在 Object 創建時觸發的 Method, 主要用於初始化不同 Object
每個 Class 中均隱藏著一個 Default Constructor
而開發者可以新增額外/覆蓋預設的 Constructor

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Grape {
float price = 10;
int quantity = 3;

/* 隱藏的 Default Constructor, 甚麼都沒有
Grape(){}
*/


// 重新定義 Default Constructor
Grape() {
// 每次創建都觸發 print 一次
System.out.println("Grape created");
}

// 自定義附帶參數 Parameter 的 Constructor
Grape(float newPrice, int newQuantity) {
price = newPrice;
quantity = newQuantity;
}
}

public class HelloWorld {

public static void main(String[] args) {
// 自動相應至無參數的 Constructor
Grape grapeA = new Grape();
// grapeA.price = 10, quantity = 3
System.out.println(grapeA.price + " " + grapeA.quantity);

// 自動相應至有參數的 Constructor
Grape grapeB = new Grape(20, 5);
// grapeB.price = 20, quantity = 5
System.out.println(grapeA.price + " " + grapeA.quantity);
}

}

執行後的 Output

1
2
3
4
5
Grape created
10.0 3
Grape created
20.0 5

筆記正在草擬...
也許到這裡你對實際的開發還沒明白透徹, 無須擔心, 稍後將會作更多解釋


實用鏈接

To be added..