abstract 修饰符
更新:2007 年 11 月
声明类必须扩展或者方法或属性的实现必须由派生类来提供。
abstract statement
参数
- statement
必选。类、方法或属性定义。
备注
abstract 修饰符用于类中不具有实现的方法或属性或者用于包含这些方法的类。具有抽象成员的类不能使用 new 运算符来实例化。您可以从抽象基类派生抽象和非抽象的类。
类中的方法和属性以及类可以使用 abstract 修饰符来标记。如果一个类包含任何 abstract 成员,则必须标记为 abstract。接口和接口的成员为隐式抽象,它们不能采用 abstract 修饰符。字段不能为 abstract。
不能将 abstract 修饰符与其他继承修饰符 (final) 组合。默认情况下,类成员既不是 abstract 也不是 final。继承修饰符不能与 static 修饰符组合。
示例
下面的示例阐释 abstract 修饰符的用法。
// CAnimal is an abstract base class.
abstract class CAnimal {
abstract function printQualities();
}
// CDog and CKangaroo are derived classes of CAnimal.
class CDog extends CAnimal {
function printQualities() {
print("A dog has four legs.");
}
}
class CKangaroo extends CAnimal {
function printQualities() {
print("A kangaroo has a pouch.");
}
}
// Define animal of type CAnimal.
var animal : CAnimal;
animal = new CDog;
// animal uses printQualities from CDog.
animal.printQualities();
animal = new CKangaroo;
// animal uses printQualities from CKangaroo.
animal.printQualities();
该程序的输出为:
A dog has four legs.
A kangaroo has a pouch.