在程式語言中,抽象類型是指名義型別系統中不能直接實例化的類型;而非抽象的類型,即可以實例化的類型,則被稱為具體類型[1]。
一個抽象類型可以不提供實現,或者不完整的實現。在一些語言中,沒有實現的抽象類型被稱為協定、介面。在類別為基編程以及物件導向程式設計中,抽象類型被實現為抽象類(也被稱為抽象基礎類別),而具體類型被實現為具體類。
//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;
}
}
參考文獻
Wikiwand in your browser!
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.