Bagikan melalui


interface Statement

Declares the name of an interface, as well as the properties and methods that comprise the interface.

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

Arguments

  • modifiers
    Optional. Modifiers that control the visibility and behavior of the property.

  • interfacename
    Required. The name of the interface; follows standard variable naming conventions.

  • implements
    Optional. Keyword indicating that the named interface implements, or adds members to, a previously defined interface. If this keyword is not used, a standard JScript base interface is created.

  • baseinterfaces
    Optional. A comma-delimited list of interface names that are implemented by interfacename.

  • interfacemembers
    Optional. interfacemembers can be either method declarations (defined with the function statement) or property declarations (defined with the function get and function set statements).

Remarks

The syntax for interface declarations in JScript is similar to that for class declarations. An interface is like a class in which every member is abstract; it can only contain property and method declarations without function bodies. An interface may not contain field declarations, initializer declarations, or nested class declarations. An interface can implement one or more interfaces by using the implements keyword.

A class may extend only one base class, but a class may implement many interfaces. Such implementation of multiple interfaces by a class allows for a form of multiple inheritance that is simpler than in other object-oriented languages, for example, in C++.

Example

The following code shows how one implementation can be inherited by multiple interfaces.

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();

The output of this program is:

This the form name.
This the form name.
This the form name.

Requirements

Version .NET

See Also

Reference

class Statement

function Statement

function get Statement

function set Statement

Other Resources

Modifiers