Udostępnij za pośrednictwem


class Statement

Declares the name of a class as well as a definition of the variables, properties, and methods that comprise the class.

[modifiers] class classname [extends baseclass] [implements interfaces]{
   [classmembers]
}

Arguments

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

  • classname
    Required. Name of the class; follows standard variable naming conventions.

  • extends
    Optional. Keyword indicating that the class classname extends baseclass. If this keyword is not used, a standard JScript base class is created that extends System.Object.

  • baseclass
    Optional. The name of the class being extended.

  • implements
    Optional. Keyword indicating that the class classname implements one or more interfaces.

  • interfaces
    Optional. A comma-delimited list of interface names.

  • classmembers
    Optional. classmembers can be method or constructor declarations (defined with the function statement), property declarations (defined with the function get and function set statements), field declarations (defined with the var or const statements), initializer declarations (defined with the static statement), enumeration declarations (defined with the enum statement), or nested class declarations.

Remarks

Classes can be used to create instances or serve as the base for other classes, depending on the modifiers of the class. If a class is marked with the abstract modifier, the class can serve as a base class for other classes to extend, but instances of an abstract class cannot be created. If a class is marked with the final modifier, instances of the class can be created with the new operator, but the class cannot serve as a base.

Methods and constructors may be overloaded in a class. Consequently, multiple methods (or constructors) may have the same names. Overloaded class members are distinguished by their unique signatures, which are comprised of the name of the member and the data type of each of its formal parameters. Overloads allow a class to group methods with similar functionality.

A class can inherit the functionality of an existing base class by using the extends keyword. Methods of the base class can be overridden by declaring a new method with the same signature as the new class method. Methods in the new class can access overridden members of the base class using the super statement.

A class can be patterned on one or more interfaces using the implements keyword. A class cannot inherit any behavior from an interface because an interface does not provide an implementation for any member. An interface does provide the class with a 'signature' that can be used when interacting with other classes. Unless the class that implements an interface is abstract, an implementation must be provided for every method defined in the interface.

Modifiers can be used to make a class instance behave more like a JScript object. To allow a class instance to handle dynamically added properties, use the expando modifier, which automatically creates a default indexed property for the class. Only expando properties are accessible using the square bracket notation of the JScript Object object.

Example 1

The following example creates a CPerson class with various fields and methods, the details of which have been omitted. The CPerson class serves as the base class for the CCustomer class in the second example.

// All members of CPerson are public by default.
class CPerson{
   var name : String;
   var address : String;

   // CPerson constuctor
   function CPerson(name : String){
      this.name = name;
   };

   // printMailingLabel is an instance method, as it uses the
   // name and address information of the instance.
   function printMailingLabel(){
      print(name);
      print(address);
   };

   // printBlankLabel is static as it does not require
   // any person-specific information.
   static function printBlankLabel(){
      print("-blank-");
   };
}

// Print a blank mailing label.
// Note that no CPerson object exists at this time.
CPerson.printBlankLabel();

// Create a CPerson object and add some data.
var John : CPerson = new CPerson("John Doe");
John.address = "15 Broad Street, Atlanta, GA 30315";
// Print a mailing label with John's name and address.
John.printMailingLabel();

The output of this code is:

-blank-
John Doe
15 Broad Street, Atlanta, GA 30315

Example 2

A CCustomer class is derived from CPerson, having additional fields and methods not applicable to a generic member of the CPerson class.

// Create an extension to CPerson.
class CCustomer extends CPerson{
   var billingAddress : String;
   var lastOrder : String;

   // Constructor for this class.
   function CCustomer(name : String, creditLimit : double){
      super(name); // Call superclass constructor.
      this.creditLimit = creditLimit;
   };

   // Customer's credit limit. This is a private field
   // so that only member functions can change it. 
   private var creditLimit : double;
   // A public property is needed to read the credit limit.
   function get CreditLimit() : double{
      return creditLimit;
   }
}

// Create a new CCustomer.
var Jane : CCustomer = new CCustomer("Jane Doe",500.);
// Do something with it.
Jane.billingAddress = Jane.address = "12 Oak Street, Buffalo, NY 14201";
Jane.lastOrder = "Windows 2000 Server";
// Print the credit limit.
print(Jane.name + "'s credit limit is " + Jane.CreditLimit);
// Call a method defined in the base class.
Jane.printMailingLabel();

The output of this part of the code is:

Jane Doe's credit limit is 500
Jane Doe
12 Oak Street, Buffalo, NY 14201

Requirements

Version .NET

See Also

Reference

interface Statement

function Statement

function get Statement

function set Statement

var Statement

const Statement

static Statement

new Operator

this Statement

super Statement

Other Resources

Modifiers