2012-01-08 147 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(); 
    } 
} 
相关问题