抽象类型
外观
类型系统 |
---|
一般概念 |
主要分类 |
次要分类 |
在编程语言中,抽象类型是指名义类型系统中不能直接实例化的类型;而非抽象的类型,即可以实例化的类型,则被称为具体类型[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;
}
}
参考文献
[编辑]- ^ 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