我有许多相同自定义类的对象,还有另一个自定义类的许多对象。我想创建一个 switch 语句来确定对象属于哪个类。以下代码无法编译,所以我不确定这是否可行。是使用 if 语句的唯一替代方法吗?
function mouseClickEventHandler(evt:MouseEvent):void
{
switch (evt.currentTarget)
{
case (is customClassA): trace("is instance of customClassA"); break
case (is customClassB): trace("is instance of customClassB");
}
}
最佳答案
这应该有效:
function mouseClickEventHandler ( evt:MouseEvent ):void
{
switch ( evt.currentTarget.constructor )
{
case CustomClassA:
trace("is instance of customClassA");
break;
case CustomClassB:
trace("is instance of customClassB");
break;
}
}
参见 Object.constructor .
关于actionscript-3 - ActionScript - 在 switch 语句中使用 "is"比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3788898/