我有了扩展我的 trace() 消息的想法。
为什么是
trace() 遍布我的代码,我想通过一个简单的命令打开/关闭它们,并且可能向 trace() 添加某种优先级功能,即
myTrace.TraceMsg("loosehere",debugme, 0);
myTrace.TraceMsg("winhere",debugme, 1);
当我运行时,仅显示优先级较高的那个(在这种情况下为“1”)。
我还想添加更多功能,例如将消息记录到文件等。
问题
trace() 如何工作? -是否可能以某种方式重载trace()? - 我将如何实现自定义 TraceMsg(这里是什么代码?)方法?
在我们最喜欢的搜索引擎上查找有关此主题的信息时遇到了一些严重的问题,因此我们将不胜感激。
最佳答案
trace()
本身是一个顶层函数,而不是一个类,所以很遗憾我们不能扩展它。话虽如此,我们可以在一个简单的类中使用它来做它通常所做的事情,只有在这种情况下,跟踪是基于条件的(即 bool 值 - true|false 等)。首先,我们创建 Trace 类,我们不会自己实例化它,因为我们正在通过下面的类 Tracer 使用工厂设计模式。 Tracer 是围绕单例设计模式构建的,但在调用 Tracer 的 trace 方法时利用工厂模式来实例化 Trace 的实例。
//This class is handled by Tracer, which is right below it.
//You WILL NOT instantiate these, nor hold references.
package
{
public class Trace
{
private function _value:*;
private function _trace:Boolean;
public function Trace(pValue:*, pTrace:Boolean):void
{
_value = pValue;
_trace = pTrace;
}
public function get value():*
{
return _value;
}
public function get trace():Boolean
{
return _trace;
}
}
}
//This is the important class and the only one you will work with.
package
{
/**
*Utilizes Singleton and Factory design patterns.
*/
public class Tracer
{
private var _traceArray:Array;
private static var _instance:Tracer;
public function Tracer(pvt:PrivateClass = null):void
{
if(pvt == null)
{
throw(new Error("You cannot instantiate this class directly, please use the static getInstance method."));
}
_init();
}
public static function getInstance():Tracer
{
if(Tracer._instance == null)
{
Tracer._instance = new Tracer(new PrivateClass());
}
return Tracer._instance;
}
public function trace(pValue:*, pTrace:Boolean):void
{
var trace:Trace = new Trace(pValue, pTrace);
if(trace.pTrace)
{
trace(pValue);
}
}
//Since we have the option for individual traces to be disabled
//I provide this to get access to any and all later.
public function traceAll():void
{
traceStr:String = _traceArray.toString();
}
public function get traceables():Array
{
return _traceArray;
}
//Here we provide a method to trace all, even if set to false in their constructor.
private function _init():void
{
_traceArray = new Array();
}
}
}
//Here we create a class that is OUTSIDE of the package.
//It can only be accessed from within this class file. We use this
//to make sure this class isn't instantiated directly.
class PrivateClass
{
function PrivateClass():void
{
trace('can only be accessed from within this class file');
}
}
//Now for use in doc class
package
{
import flash.display.Sprite;
import flash.events.Event;
//No need to import Tracer and Trace, they are also in the
//unnamed package.
public class DocumentClass extends Sprite
{
private var _tracer:Tracer;
public function DocumentClass():void
{
if(stage) _init();
else addEventListener(Event.ADDED_TO_STAGE, _init);
}
private function _init(e:Event = null):void
{
_tracer = Tracer.getInstance();
_tracer.trace(10*20, false);
_tracer.trace(10*20, 0); //SAME AS ABOVE
_tracer.trace("I love AS3", true); //traces
_tracer.traceAll(); //Would trace: 200, 200, I love AS3
}
}
}
请记住,这是时髦的,很可能会有一两个错误,但是想法就在那里了。也就是说,这没有经过测试,只是为了让您了解如何实现这一点。
我希望这有帮助。
关于actionscript-3 - 在 AS3 中创建自定义 trace() 类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/715738/