使用abstract修飾符定義不能實例化的抽象類。抽象類對于定義接口非常有用。如果您希望抽象類看起來是可實例化的,請定義一個工廠構造函數。
抽象類通常有抽象方法。這里有一個聲明抽象類的例子,它有一個抽象的方法:
~~~
// This class is declared abstract and thus
// can't be instantiated.
abstract class AbstractContainer {
// Define constructors, fields, methods...
void updateChildren(); // Abstract method.
}
~~~