抽象類型

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書

程式語言中,抽象類型是指名義型別系統中不能直接實例化的類型;而非抽象的類型,即可以實例化的類型,則被稱為具體類型[1]

一個抽象類型可以不提供實現,或者不完整的實現。在一些語言中,沒有實現的抽象類型被稱為協定介面。在類別為基編程以及物件導向程式設計中,抽象類型被實現為抽象類(也被稱為抽象基礎類別),而具體類型被實現為具體類。

範例(Java)[編輯]

//By default, all methods in all classes are concrete, unless the abstract keyword is used.
abstract class Demo {
    // An abstract class may include abstract methods, which have no implementation.
    abstract public int sum(int x, int y);

    // An abstract class may also include concrete methods.
    public int product(int x, int y) { return x*y; }
}

//By default, all methods in all interfaces are abstract, unless the default keyword is used.
interface DemoInterface {
    [abstract] int getLength(); //Abstract can be used here, though is completely useless
    
    //The default keyword can be used in this context to specify a concrete method in an interface
    default int product(int x, int y) {
        return x * y;
    }
}

參考文獻[編輯]

  1. ^ Mitchell, John C.; Plotkin, Gordon D.; Abstract Types Have Existential Type頁面存檔備份,存於互聯網檔案館), ACM Transactions on Programming Languages and Systems, Vol. 10, No. 3, July 1988, pp. 470–502