2013年7月23日 星期二

Collection:SML

大概分三種: [記法:衣服的尺寸SML] 都是Interface

  1. Set
  2. Map
  3. List
最大尺寸Set 跟 最小尺寸List 都屬於Collection,一般尺寸屬於Map

  • Set<T>
不可重複
  • List<T>
可重複
  • Map<K, V> 
一個 key 最多只能對應一個 value


常用整理:

2013年7月22日 星期一

Array and For-loop

//宣告一個array
int[] integers = {6, 7, 8, 9};
//寫法1
for (int j = 0; j<integers.length; j++){
int i = integers[j];
System.out.println(i);/* 依次输出“0”、“1”、“2”、“3” */
}
//寫法2
for (int i : integers) {
System.out.println(i);/* 依次输出“6”、“7”、“8”、“9” */
}

2013年7月20日 星期六

泛型


  • BooleanFoo.java

public class BooleanFoo {
    private Boolean foo;
    public void setFoo(Boolean foo) {
        this.foo = foo;
    }
    public Boolean getFoo() {
        return foo;
    }
}

  • IntegerFoo.java

public class IntegerFoo {
    private Integer foo;
    public void setFoo(Integer foo) {
        this.foo = foo;
    }
    public Integer getFoo() {
        return foo;
    }
}

類別定義時邏輯完全一樣,只是當中宣告的成員型態不同,有點小聰明的程式設計人員會將第一個類的內容複製至另一個檔案中,然後用編輯器「取代」功能一次取 代所有的型態名稱(即將Boolean取代為Integer)。

OK!是有些小聰明,但還是不太聰明,如果類別中的邏輯要修改,您要修改兩個檔案,泛型(Generics)的需求就在此產生



  • 以ArrayList<E>为例:<>念着typeof

connect to db

順序:
  • new connection連DB
Connection conn = null;
  • new PerparedStatement將SQL語法組好
PreparedStatement pstmt = null;

  • new ResultSet接收SQL回傳的查詢結果

ResultSet rs = null;

interface and abstract class


  • interface
Interface不能Instance,不能有建構式、成員。
不能有修飾詞,Public、Private…等。
不能宣告為共用成員。
實作Interface必須實作Interface所有方法及屬性。
一個Class可實作多個Interface。
  • abstract
Abstract Class不能Instance。即不能使用New關鍵字初始化。
Abstract Class是必須被衍生Class覆寫方法。
衍生Class可以部份實作。
如果Class中包含Abstract Method(抽象方法),那麼此Class就必須定義為Abstract Class。
一個Class只能繼承一個Abstract Class。


  • 相同:
兩者都不能被具體化。
都必須去實作已宣告的『抽象』方法。
  • 相異:
一個類別能實作很多介面,但只能繼承一個抽象類別。
介面所定義的每個方法都必須在延伸類別中實作,抽象類別則可以定義部分的方法是已經具有此方法的內容描述,而部分是定義必須在延伸類別中去實作的。這麼說或許讀者會與我早先說明的介面與抽象類別『都必須去實作已宣告抽象的方法』感到疑惑,這部分會在稍後做說明。
介面強調的是定義一些功能給較不相干的類別使用,抽象類別主要是應用在關係密切(is a)的類別中使用。
抽象類別一定屬於繼承架構,而且一定是父類別,介面可以讓毫無關係的類別來實作同一個介面,這點同時也呼應了第三點的說明。


  • 結論:
除了實際功能面的差異外,另外Abstract Class一般都是在Class程式碼撰寫過程慢慢被發現,
進而從Class提升為Abstract Class,我們應盡量讓Abstract Class擁有最多的共用程式碼,盡量減少資料。

而Interface比較像定規格,一般而言都是一開始就設計、定義,也是另一種的「藍圖」,
一開始在什麼都不知道的情況下,預先設計好相關架構。

多型



class ClassA {
void method1() {
System.out.println("method1() in ClassA");
}
void method2() {
System.out.println("method2() in ClassA");
}
}

class ClassB extends ClassA {
void method1() {
System.out.println("method1() in ClassB");
}
void method3() {
System.out.println("method3() in ClassB");
}
}

public class ClassC {
public static void main(String[] args) {
ClassA a = new ClassB();
a.method1(); // method1() in ClassB
a.method2(); // method2() in ClassA
a.method3(); // compile-time error
}
}


object a reference type is ClassA

object a obect type is ClassB

throw and throws


  • throw new exception(接收該method的exception)
  • throws exception(該method不處理execption, retrun to caller)
而你不使用try..catch處理,則表示目前環境資訊不足以處理exception,必須由屆時使用該方法的客戶端依當時環境資訊進行處理,為了告訴編譯器這件事實,則必須在method上使用throws宣告會丟出exception,那麼compiler才會讓你通過
  • StackTrace

  1. try{ 
  2.  int x = 5/0; 
  3.  } catch(Exception e){ 
  4.  e.printStackTrace(); 
  5.  }



Example:


public HxAccg getHxAccgByAccNo(String acc_no) throws Exception
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        HxAccg record = null;
        try
        {
            conn = getDataSource().getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM HD_ACCG_ACT WHERE ACC_NO = ? ");
            pstmt.setString(1, acc_no);
            rs = pstmt.executeQuery();
            if (rs.next())
            {
                record = fillHxAccgByResultSet(rs);
            }
        }
        catch (Exception ex)
        {
        loggor.error("依照存取單號取得報到資料發生錯誤,存取單號="+acc_no, ex);
        throw new Exception("依照存取單號取得報到資料發生錯誤,存取單號="+acc_no,ex);
        }
        finally
        {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        }

        return record;

    }

Return and Break and Contiune


  • break
// Using break to exit a loop.
class BreakLoop {
 public static void main(String args[]) {
 for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
 System.out.println("i: " + i);
 }
System.out.println("Loop complete.");
}
}

  • contiune
// Demonstrate continue.
class Continue {
 public static void main(String args[]) {
 for(int i=0; i<10; i++) {
 System.out.print(i + " ");
 if (i%2 == 0)
   continue;
System.out.println("");
}
}
}

  • return
// Demonstrate return.
class Return {
public static void main(String args[]) {
 boolean t = true;
System.out.println("Before the return.");
 if(t) return; // return to caller
 System.out.println("This won't execute.");
}
}