2012-01-08 145 views
0

這個問題有點愚蠢。我正在嘗試實施技能更新系統。所以要解釋。如何將參考班級傳遞到另一個班級

有一類

class AppInfo 
{ 
    public static var power:int = 10; 
    public static var speed:int = 20; 

} 

和類SmartButton應當採用參照例如靜態變量中的一個在構造函數中加電,並在給定值上增加它。

例如

class SmartButton 
{ 
    public function onClick(skillReference:int = <AppInfo.power>, incrementVAlue:int = 10) 
    { 
     skillReference += incrementVAlue 

    } 

} 

我想要此代碼更新AppInfo類中的電源值。但是,這並沒有發生...我假設,因爲技能通過價值不作爲參考...

你可以建議一種解決任務的方式?

謝謝

回答

2

你的假設是正確的,ints是通過值而不是參考。一個直接的方法是封裝功率轉換成一個引用類型(一個class),而不是一個值類型:

class Skill { 
    public var value:int; 

    public function Skill(val:int) { 
     this.value = val; 
    } 
} 

class AppInfo 
{ 
    public static var power:Skill = new Skill(10); 
    public static var speed:Skill = new Skill(20); 

} 

然後使power應該將它作爲對實例的引用。儘管您必須稍微更改一下實現,才能使用skillReference.value

除此之外,我認爲有幾種方法可以抽象出你想要的結果。一種方法是使用接口並利用一些依賴注入。

interface ISkills 
{ 
    function get power():int; 
    function set power(val:int):void; 
} 

class AppInfo implements ISkills 
{ 
    private static _power:int = 0; 

    public function get power():int { return _power; } 
    public function set power(val:int):void { _power = val; } 
} 

class SmartButton 
{ 
    public function onClick(skills:int = ISkills, skill:String = "power", incrementVAlue:int = 10) 
    { 
     skills[skill] += incrementVAlue 
    } 
} 

這裏的想法是,你想從你的實現中解耦你的用法。在這種情況下SmartButton並不需要知道技能如何工作只是如何操作他們。它失去了對靜態類AppInfo的支持,轉而採用可注入的實例。這種方法有一些優點,如果你決定靜態類不是最好的實現思想,而不必更新一堆類/代碼,那麼它可以更容易地進行測試,並且可以更容易地交換實現。另外,不是將ISkills注入方法中,而是將其注入到SmartButton的構造函數中,並保留對技能容器的引用private

另一種方法是使用功能方法。

class SmartButton 
{ 
    public var defaultWorker:Function = function(val:int):void { 
     AppInfo.power += val; 
    } 

    public function onClick(worker:Function = undefined, incrementValue:int = 10):void 
    { 
     if(worker == undefined) worker = defaultWorker; 
     worker.call(this, incrementValue); 
    } 
} 

再次,在這種情況下,而不是緊密耦合的實現直接使用AppInfo類,你注入一個「工人」它爲你做的工作(如果工人是undefined則使用默認的工人。然後你可以通過改變傳入的閉包來換出哪些屬性被更改。例如,如果你想改變speed,而不是您需要調用:

var smartButton:SmartButton; 
smartButton.onClick(function(val:int):void { AppInfo.speed += val}); 

沒有這麼簡潔的,因爲它可以,但它能夠完成任務。

0

我可能會以稍微不同的方式實現這一點。

也許類似於;

class Properties { 
private var _properties:Dictionary = new Dictionary(); 

    public function setValue(key:String, value:int) { 
     _properties[key] = value; 
    } 

    public function getValue(key:String):int { 
     if(!_properties[key]) return 0; 
     else return _properties[key]; 
    } 

    public function modifyValue(key:String, value:int) { 
     setValue(key, getValue(key) + value); 
    } 
} 

class SmartButton 
{ 
    public function onClick(target:Properties, key:String, incrementValue:int = 10) { 
     target.modifyValue(key, incrementValue); 
    } 
} 

或沿着這些線。

1

使用command pattern強制性的 「優雅成熟的」 的方法:

Interface Command { 
    function execute():void; 
} 

Class UpdatePower implements Command { 
    private var appInfo:AppInfo; 
    private var delta:int; 

    public function UpdatePower(appInfo:AppInfo, delta:int) { 
     this.appInfo = appInfo; 
     this.delta = delta; 
    } 

    public function execute():void { 
     appInfo.delta += delta; 
    } 
} 

Class SmartButton { 
    var command:Command; 

    public function SmartButton(command:Command) { 
     this.command = command; 
    } 

    public function onClick(event:Event):void { 
     command.execute(); 
    } 
} 
相關問題