본문 바로가기

AS

프로퍼티를 클래스로...

package com.example.programmingas3.playlist
{
    /**
     * A pseudo-enum representing the different properties by which a Song instance
     * can be sorted.
     */
    public final class SortProperty
    {
        // ------- Public Constants -------
       
        // These constants represent the different properties
        // that the list of songs can be sorted by,
        // providing a mechanism to map a "friendly" name to the actual property name
        public static const TITLE:SortProperty = new SortProperty("title");
        public static const ARTIST:SortProperty = new SortProperty("artist");
        public static const YEAR:SortProperty = new SortProperty("year");

        // 황당하다.. 저렇게 할 수 있다니. 재귀적 같은데 된다.

       
        // ------- Private Variables -------
        private var _propertyName:String;


        // ------- Constructor -------
       
        public function SortProperty(property:String)
        {
            _propertyName = property;
        }


        // ------- Public Properties -------
       
        public function get propertyName():String
        {
            return _propertyName;
        }
    }
}

이 클래스는 fla 파일이나 다른 as 파일에서

function buttonListener(e:MouseEvent):void {
    var buttonClicked:Button = e.target as Button;
    // 여러 버튼의 이벤트리스너를 이 함수로 지정하고
    // 스위치 문에 버튼 오브젝트를 걸어서 이렇게 바로 처리할 수 있다.
    switch(buttonClicked) {
        case sortByTitle:
            sortList(SortProperty.TITLE);
            break;
        case sortByArtist:
            sortList(SortProperty.ARTIST);
            break;
        case sortByYear:
            sortList(SortProperty.YEAR);
            break;
        case showAddControlsBtn:
            setFormState(ADD_SONG);
            break;
    }
}

SortProperty.TITLE 처럼 상수처럼 사용할 수 있다.