class 문

클래스의 이름과 클래스를 구성하는 변수, 속성, 메서드의 정의를 선언합니다.

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

인수

  • modifiers
    선택적 요소로서, 클래스의 표시 유형과 동작을 제어하는 한정자입니다.

  • classname
    필수적 요소로서, class의 이름입니다. 표준 변수 명명 규칙을 따릅니다.

  • extends
    선택적 요소로서, 클래스 classnamebaseclass를 확장함을 나타내는 키워드입니다. 이 키워드가 사용되지 않으면 System.Object를 확장하는 표준 JScript 기본 클래스가 만들어집니다.

  • baseclass
    선택적 요소로서, 확장되는 클래스의 이름입니다.

  • implements
    선택적 요소로서, 클래스 classname이 하나 이상의 인터페이스를 구현함을 나타내는 키워드입니다.

  • interfaces
    선택적 요소로서, 인터페이스 이름의 쉼표로 구분된 목록입니다.

  • classmembers
    선택적 요소로서, classmembers는 메서드 또는 생성자 선언(function 문으로 정의됨), 속성 선언(function getfunction set 문으로 정의됨), 필드 선언(var 또는 const 문으로 정의됨), 이니셜라이저 선언(static 문으로 정의됨), 열거형 선언(enum 문으로 정의됨) 또는 중첩 클래스 선언이 될 수 있습니다.

설명

클래스의 한정자에 따라 클래스를 인스턴스를 만드는 데 사용하거나 다른 클래스의 기본으로 사용할 수 있습니다. 클래스에 abstract 한정자가 표시되면 다른 클래스를 확장하기 위한 기본 클래스로 사용할 수 있지만 abstract 클래스의 인터페이스는 만들 수 없습니다. 클래스에 final 한정자가 표시되면 new 연산자를 사용하여 클래스의 인스턴스를 만들 수 있지만 해당 클래스는 다른 클래스의 기본으로 사용할 수는 없습니다.

메서드 및 생성자는 클래스에서 오버로드할 수 있습니다. 따라서 여러 메서드(또는 생성자)가 같은 이름을 가질 수 있습니다. 오버로드된 클래스 멤버는 멤버의 이름 및 해당 형식 매개 변수 각각의 데이터 형식으로 구성되는 고유한 시그니처로 구별됩니다. 오버로드를 통해 클래스에서는 유사한 기능의 메서드를 그룹화할 수 있습니다.

클래스는 extends 키워드를 사용하여 기존의 기본 클래스의 기능을 상속할 수 있습니다. 기본 클래스의 메서드는 새 클래스 메서드와 같은 시그니처로 새 메서드를 선언하여 재정의할 수 있습니다. 새 클래스의 메서드는 super 문을 사용하여 기본 클래스의 재정의된 멤버에 액세스할 수 있습니다.

implements 키워드를 사용하여 하나 이상의 인터페이스를 기본으로 클래스를 만들 수 있습니다. 인터페이스에서는 멤버를 구현할 수 없으므로 클래스는 인터페이스에서 동작을 상속할 수 없습니다. 인터페이스는 다른 클래스와 상호 작용할 때 사용할 수 있는 '시그니처'를 클래스에 제공합니다. 인터페이스를 구현하는 클래스가 abstract가 아닌 경우 인터페이스에 정의된 모든 메서드를 구현해야 합니다.

클래스 인스턴스가 JScript 개체와 좀 더 가깝게 동작하도록 하기 위해 한정자를 사용할 수 있습니다. 클래스 인스턴스에서 동적으로 추가된 속성을 처리할 수 있게 하려면 클래스의 인덱싱된 기본 속성을 자동으로 만드는 expando 한정자를 사용합니다. JScript Object 개체의 대괄호 표기법을 사용하면 expando 속성에만 액세스할 수 있습니다.

예제 1

다음 예제에서는 다양한 필드와 메서드 및 여기에서는 생략된 세부 사항으로 CPerson 클래스를 만듭니다. CPerson 클래스는 두 번째 예제에서 CCustomer 클래스의 기본 클래스로 작용합니다.

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

이 코드는 다음과 같이 출력됩니다.

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

예제 2

CCustomer 클래스는 CPerson에서 파생되며 CPerson 클래스의 일반 멤버에 적용되지 않는 추가 필드와 메서드를 갖습니다.

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

이 코드 부분은 다음과 같이 출력됩니다.

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

요구 사항

.NET 버전

참고 항목

참조

interface 문

function 문

function get 문

function set 문

var 문

const 문

static 문

new 연산자

this 문

super 문

기타 리소스

한정자