抽象类型

编程语言中,抽象类型是指名義型別系統中不能直接实例化的类型;而非抽象的类型,即可以实例化的类型,則被称为具体类型[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