資料抽象化與封裝化 (Data Abstraction & Encapulation)

資料封裝化(data abstraction)

資料封裝化或資訊隱藏(information hiding) 是指 將資料物件的內部程式碼之細節與外界隔絕

資料抽象化(data encapulation)

資料抽象化是 將一個資料物件的規格與它內部實作分開

資料型態(data type)

資料型態是 一些物件以及一組處理這些物件的運算所組成的集合

抽象化資料型態(abstract data type. ADT)

組織過的資料型態,得以將物件、物件之運算兩者的規格,跟物件的實際表示法、運算的實作方法分開

ADT exercise

  1. 在 NaturalNumber ADT 裡面增加下列運算: Predecessor、IsGreat、Multiply、Divide
    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    ADT NaturalNumber is
    object:從零開始到電腦上的最大整數(MAXINT)為止所形成的一串
    有順序的整數子範圍
    fuctions:對所有的x,y∈NaturalNumber;TRUE,FALSE∈Boolean
    並且+、-、<、==、以及=是一班的整數運算子

    zero():NaturalNumber ::= 0
    IsZero(x):Boolean ::=
    if(x == 0)IsZero = true
    else IsZero = false

    Add(x,y):NaturalNumber ::=
    if( x + y <= MAXINT) Add = x+y
    else Add = MAXINT

    Equal(x,y):Boolean ::=
    if(x == y) Equal=TRUE
    else Equal=FALSE

    Succesor(x):NaturalNumber ::=
    if(x == MAXINT) Succesor = x
    else Succesor = x + 1

    Subtract(x,y):NaturalNumber ::=
    if(x < y) Subtract = 0
    else Subtract = x - y

    //Predecessor
    Predecessor(x):NaturalNumber ::=
    if(x < 0) return ERROR
    if(x == 0) return 0
    return x - 1

    //IsGreater
    IsGreater(x,y):Boolean ::=
    if ((x -y) < 0) return ERROR
    if ((x -y) == 0) return FALSE
    return TRUE

    //Multiply
    Multiply(x,y):NaturalNumber ::=
    if(x == 0 or y == 0) return 0
    if(MAXINT / x < y) return MAXINT
    return x * y

    //Divide
    Divide(x,y):NaturalNumber ::=
    if(y == 0) return ERROR
    if(x == 0) return 0
    if(x < y) return 0
    return x / y
  2. 創造一個 ADT,Set。使用標準的數學定義並且包含下列的運算 : 新生一個集合(Create)、插入(Insert)、移除(Remove)、屬於(IsIn)、聯集(Union)、交集(Intersection)、相減(Difference)
    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
    ADT Set is
    object:具有獨特元素且沒有順序的集合
    functions:對所有的x,y∈Set;e∈Element;TRUE,FALSE∈Boolean

    // 新建一個空集合
    Create(): Set ::=
    Create an empty set and return it

    // 插入元素到集合
    Insert(x, e): Set ::=
    Add element e to set x and return the updated set

    // 從集合中移除元素
    Remove(x, e): Set ::=
    Remove element e from set x and return the updated set

    // 檢查元素是否屬於集合
    IsIn(x, e): Boolean ::=
    if e is in x, return TRUE
    else return FALSE

    // 計算兩個集合的聯集
    Union(x, y): Set ::=
    Create a new set that contains all elements from x and T and return it

    // 計算兩個集合的交集
    Intersection(x, y): Set ::=
    Create a new set that contains elements present in both x and T and return it

    // 計算兩個集合的差集
    Difference(x, y): Set ::=
    Create a new set that contains elements in x but not in T and return it

  3. 創造一個 ADT,Bag。在數學上bag跟集合(set)很相似,但是bag可以包含重複的元素。至少必須包括下列的運算 : 新生一個bag(Crete)、插入(Insert)、刪除(Remove)、屬於(IsIn)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    ADT Bag is
    object:具有獨特元素及重複元素的集合
    functions:對所有的x,y∈Bag;e∈Element;TRUE,FALSE∈Boolean

    // 新建一個空的 Bag
    Create(): Bag ::=
    Create an empty bag and return it

    // 插入元素到 Bag
    Insert(x, e): Bag ::=
    Add element e to bag x and return the updated bag

    // 從 Bag 中刪除元素
    Remove(x, e): Bag ::=
    Remove one occurrence of element e from bag x and return the updated bag
    (if e is not in x, return x unchanged)

    // 檢查元素是否屬於 Bag
    IsIn(x, e): Boolean ::=
    if e is in x, return TRUE
    else return FALSE