diary       blog       guest

사용자 정의 클래스 만들기

http://www.action-scripter.com/blog/trackback/985
아래 코드를 사용자 정의 클래스로 만들어 보겠습니다.

GeSHi © 2004, Nigel McNie
  1. function Product(id:Number, prodName:String, price:Number){
  2.     this.id = id;
  3.     this.prodName = prodName;
  4.     this.price = price;
  5. };
Parsed in 0.031 seconds

ActionScript 2.0에서 클래스를 제대로 정의하려면 class 키워드로 모든 클래스를 묶고 생성자 외부의 생성자에 모든 변수를 선언해야 합니다.

GeSHi © 2004, Nigel McNie
  1. class Product{
  2.     // variable declarations
  3.     var id:Number
  4.     var prodName:String
  5.     var price:Number
  6.     // constructor
  7.     function Product(id:Number, prodName:String, price:Number){
  8.         this.id  = id;
  9.         this.prodName = prodName;
  10.         this.price = price;
  11.     };
  12. };
Parsed in 0.071 seconds

이 클래스에서 객체를 만들려면 이제 다음 코드를 사용할 수 있습니다.

GeSHi © 2004, Nigel McNie
  1. var cliplessPedal:Product = new Product(1,"Clipless Pedal",11);
  2. var monkeyBar:Product = new Product(2,"Monkey Bar",10);
Parsed in 0.010 seconds

그러나 ActionScript 2.0에서, 클래스 구조에 속하는 변수에는 직접 액세스하면 안 됩니다. 이러한 변수에 직접 액세스하는 메서드를 클래스 내에 작성하십시오. 속성을 가져오고 설정하는 메서드가 따로 있어야 합니다. 이러한 메서드를 "getter" 및 "setter" 메서드라고 합니다. 메서드의 반환 값과 메서드가 호출될 때 메서드에 제공되는 매개 변수에 대한 데이터 유형을 지정해야 합니다.

GeSHi © 2004, Nigel McNie
  1. class Product {
  2.         var id:Number;
  3.         var prodName:String;
  4.         var description:String
  5.        
  6.         function Product(id:Number, prodName:String, description:String){
  7.             setID(id);
  8.             setProdName(prodName);
  9.             setDescription(description);
  10.         };
  11.         public function setID(id:Number):Void{
  12.             this.id = id;
  13.         };
  14.         public function setProdName(prodName:String):Void{
  15.             this.prodName = prodName;
  16.         };
  17.         public function setDescription(description:String):Void{
  18.             this.description = description;
  19.         };
  20.         public function getID():Number{
  21.             return id;
  22.         };
  23.         public function getProdName():String{
  24.             return prodName;
  25.         };
  26.         public function getDescription():String{
  27.             return description;
  28.         };
  29. };
Parsed in 0.030 seconds

이 코드를 Product.as로 저장한 후 다음과 같이 객체를 만듭니다.

GeSHi © 2004, Nigel McNie
  1. var handleBars:Product = new Product(1, "ATB",
    "Available in comfort and aero design");
  2. var pedals:Product = new Product(0,"Clipless Pedals","Excellent cleat engagement");
  3. trace(pedals.getDescription());
Parsed in 0.013 seconds

그럼 output 패널에 pedals의 설명이 표시됩니다.

■ Flash 자습서 中
2005/12/29 22:03 2005/12/29 22:03
   1   ... 636  637  638  639  640  641  642  643  644   ... 1285    
ABOUT  |  WORKS  |  @seonggyu
COPYRIGHT ⓒ 2000 - 2010. ACTION-SCRIPTER.COM. ALL RIGHTS RESEVED.