2015-01-26 4 views
0

시나리오 :ActionScript 3.0의 클래스는 확장

내가 확장 클래스 characterObject 및 N 클래스가

(고양이, 개, 새를, N ...)

public class characterObject 
{ 
    public static var totalCounter:int; 
} 
public class cat extends characterObject 
public class dog extends characterObject 
public class bird extends characterObject 

가 확장 된 클래스를 위해 가능 이벤트가 있거나없는 메인 클래스의 정적 변수 (예 : totalCounter)의 변경 사항을 듣습니까?

+0

귀하의 질문은 명확하지 않습니다. 당신은 무엇을하려하십니까? 가능한 경우 몇 가지 예제 코드를 제공하십시오. – CyanAngel

+0

@CyanAngel 자, 죄송합니다. 나는 클래스 characterObj를 확장하는 클래스 cat, dog, bird (그리고 others ...)를 가진다. characterObj 내부의 정적 변수 totalCounter는 cat, dog, bird 클래스의 함수에 의해 증가 또는 감소합니다 ... cat, dog 및 bird에서 totalCounter의 변경 값을 동시에 수신 할 수 있습니까? 그래서 모두 변화에 대한 가치를 알고 있습니까? –

답변

0

getter/setter 함수와 이벤트를 조합하여 사용할 수 있습니다.

Getscript 및 Setters는 변수와 마찬가지로 액세스 할 수있는 함수로, 변수 변경시 함수를 실행할 수있는 기능을 제공합니다. 우리가 internalDispatcher라는 protected static 이벤트 디스패처를 볼 수 있듯이

public class characterObject 
{ 
    private static var _totalCounter:int; 
    protected static var internalDispatcher:EventDispatcher = new EventDispatcher(); 
    public static function get totalCounter():int 
    { 
     return _totalCounter; 
    } 
    public static function set totalCounter(value:int):void 
    { 
     _totalCounter = value; 
     var event:Event = new Event("totalCounterChanged"); 
     internalDispatcher.dispatchEvent(event); 
    } 
} 

, 우리는 우리가 characterObject 클래스를 확장 한 경우에만이 객체에서 이벤트를 수신 할 수 있습니다. getter/setter 뒤에 실제로 totalCounter을 숨김으로써 뭔가 바뀔 때마다 이벤트를 보낼 수 있습니다.

우리는 우리의 확장 클래스에서이 이벤트를 수신 할 수 있습니다

public class cat extends characterObject 
{ 
    public function cat() 
    { 
     super(); 
     internalDispatcher.addEventListener("totalCounterChanged",totalChangedHandler); 
    } 
    public function totalChangedHandler(event:Event):void 
    { 
     //Your code here; 
    } 
} 

을 그것이 어쩌면 더 나은 이벤트 핸들러는 변경없이 여러 번 실행되는 동일한 코드를 줄이기 위해 정적 수 있습니다 귀하의 요구 사항에 따라 출력.

+0

완벽하게 작동합니다;) –

관련 문제