我正在重新审视一些过滤 XML 的旧代码,但这可以很容易地应用于方法的参数(我使用它的方式,本质上是这样)。这是一个问题,我觉得我遇到了很多并且不知道解决这个问题的好方法。
所以问题是我有3个论点。它们都是可选的。我想看看哪些是存在的,并测试它们的值是否基于哪些存在(根据可能性排序):
var shiftDown : Boolean = false;
var controlDown : Boolean = false;
if ( "@shift" in x )
{
shiftDown = Global.stringToBoolean( x.@shift.toString() );
}
if ( "@control" in x )
{
controlDown = Global.stringToBoolean( x.@control.toString() );
}
if ( "@code" in x && "@shift" in x && "@control" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ x.@code.toXMLString().toUpperCase() ] ) && ( KeyManager.shiftKey == shiftDown ) && ( KeyManager.controlKey == controlDown ) )
{
...
}
}
else if ( "@code" in x && "@shift" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ x.@code.toXMLString().toUpperCase() ] ) && ( KeyManager.shiftKey == shiftDown ) )
{
...
}
}
else if ( "@code" in x && "@control" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ x.@code.toXMLString().toUpperCase() ] ) && ( KeyManager.controlKey == controlDown ) )
{
...
}
}
else if ( "@code" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ x.@code.toString().toUpperCase() ] ) )
{
...
}
}
else if ( "@shift" in x )
{
if ( KeyManager.shiftKey == shiftDown )
{
...
}
}
else if ( "@control" in x )
{
if ( KeyManager.controlKey == controlDown )
{
...
}
}
else if ("@control" in x ) && ( "@shift" in x ) )
{
if ( ( KeyManager.shiftKey == shiftDown ) && ( KeyManager.controlKey == controlDown ) )
{
...
}
}
我觉得必须有一种更短的方法来写这个,并以当前的形式重复如此多的内容。有人可以建议一种更清洁、更有效的方式来编写这个吗?
谢谢你的想法。
编辑: if 语句顺序错误。改变了那个。
这可以概括。为了清楚起见,我只是包括我的代码。如果一般问题仍然不清楚,我的印象是:
测试所有可选参数组合的最干净/最有效的方法是什么?