类的关键字

interface
override
abstract
JScript .NET  

interface 语句

声明接口的名称以及组成接口的属性和方法。

[modifiers] interface interfacename [implements baseinterfaces] {
[interfacemembers]
}

参数

modifiers
可选。控制属性的可见性和行为的修饰符。
interfacename
必选。interface 的名称;遵循标准的变量命名规则。
implements
可选。关键字,指示命名接口实现(或将成员添加到)先前定义的接口。如果未使用此关键字,则将创建一个标准的 JScript 基接口。
baseinterfaces
可选。由 interfacename 实现的接口名称的逗号分隔列表。
interfacemembers
可选。interfacemembers 可以是方法声明(用 function 语句定义)或属性声明(用 function getfunction set 语句定义)。

备注

JScript 中 interface 声明的语法类似于 class 声明的语法。接口类似于其中的每个成员都为 abstract class;它只能包含无函数体的属性和方法声明。interface 不能包含字段声明、初始值设定项声明或嵌套类声明。一个 interface 可以使用 implements 关键字来实现一个或多个 interface

一个 class 只能扩展一个基 class,但一个 class 可以实现多个 interface。一个 class 实现多个 interface 的这种方式使您可以通过比其他面向对象的语言(如 C++)更简单的形式来实现多继承。

示例

以下代码显示多个接口如何继承一个实现。

interface IFormA {
function displayName();
}
// Interface IFormB shares a member name with IFormA.
interface IFormB {
function displayName();
}
// Class CForm implements both interfaces, but only one implementation of
// the method displayName is given, so it is shared by both interfaces and
// the class itself.
class CForm implements IFormA, IFormB {
function displayName() {
print("This the form name.");
}
}
// Three variables with different data types, all referencing the same class.
var c : CForm = new CForm();
var a : IFormA = c;
var b : IFormB = c;
// These do exactly the same thing.
a.displayName();
b.displayName();
c.displayName();

该程序的输出为:

This the form name.
This the form name.
This the form name.
原文地址:https://www.cnblogs.com/wzyexf/p/364205.html