如何使函数返回*,星号,我的意思是函数能够返回任何东西,然后我可以相应地对它进行类型转换?
private function get_current_files_view():TileList
{
var tlist:TileList;
//switch(this[fm_model.files_display_type])
switch(vs_file_display.selectedChild)
{
case cvs_user_files:
tlist = tlist_files;
break;
case bx_shared_data:
tlist = tlist_shared_with_me;
break;
default:
throw new Error('Invalid container found');
}
return tlist;
}
假设在这个函数中,我希望这个函数返回tilelist和datagrid(根据情况)应该改变什么。
Plz让我知道 谢谢。
答案 0 :(得分:11)
假设在这个函数中,我想要这个 函数返回tilelist和 datagrid(根据情况)是什么 应该改变。
如果是这种情况,那么你应该返回ListBase,它是List和DataGrid的父类。
我希望能够返回任何对象,您可以将返回类型指定为Object。
如果你真的想要返回任何内容,包括整数值,例如整数,你可以使用*,如另一个答案所述。如果这真的是你需要的,我的直觉是你可能需要重构你的应用程序。
答案 1 :(得分:8)
您无需定义返回类型。如果你不这样做,那么你可以退回任何东西(不推荐):
private function get_current_files_view() { }
或者,你可以定义它必须返回一些东西,但任何东西
private function get_current_files_view():* { }
或者,如果您想要具体,可以使用接口或基类:
private function get_current_files_view():ISomeInterface {}
这有帮助吗?或者我完全误解了你的问题?
答案 2 :(得分:2)
private function whatsMyObject:(parameter:*):*
{
switch (parameter.constructor)
{
case TileList: trace("parameter is TileList");
break;
case DataGrid: trace("parameter is DataGrid");
break;
default: trace("parameter is neither TileList nor DataGrid");
}
return parameter;
}