0

我试图弄清楚如何在 case 语句中应用 is 操作数或 instanceof 来确定接口对象也属于哪种数据类型。不断收到错误

switch (IOjbect is)
            {
            case Tile:
            trace ("Load Tile");
            break;
            case House:
            trace ("A House Load");
            break;
            default:
            trace ("Neither a or b was selected")
            }

有人有想法么

4

2 回答 2

2

你不能使用 ais你正在尝试做的事情switch/case

改用 If:

var myObject:IObject=...
if (myObject is Tile){
 var myTile:Tile=Tile(myObject); 
 // you can cast myObject to Tile since the IS return true
 // otherwise it will raise an exception 
} else if (myObject is House){
 var myHouse:House=House(myObject);
}

如果它不是您想要的类型,as它将返回:null

var myObject:IObject=...
var myHouse:House=myObject as House;
 if (myHouse===null){
  var myTile:Tile=myObject as Tile;
  if (myTile===null) ...
 }
于 2009-12-30T21:05:44.773 回答
1

不幸的是,switch case 语句不能这样工作。您只能将简单对象放在 switch 语句中(而不是代码)。

对于您要完成的工作,我会使用 if/else 语句。

于 2009-12-30T21:07:57.250 回答