0

我正在重新审视一些过滤 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 语句顺序错误。改变了那个。

这可以概括。为了清楚起见,我只是包括我的代码。如果一般问题仍然不清楚,我的印象是:

测试所有可选参数组合的最干净/最有效的方法是什么?

4

1 回答 1

0

我将在这里做一些假设。从您的示例中,我认为您想要基于按键执行任务。XML 似乎包含某种首选项,无论您要启用还是禁用某些键。您有两个确定的键,shift 和 control,以及一个基于键代码的通配符键。如果这些假设是正确的,您应该能够通过将偏好测试和实际关键测试组合在一行中来缩短内容。

var shiftIsDown:Boolean = x.@shift.length() ? KeyManager.keyIsDown( KeyManager [ x.@shift.toString().toUpperCase() ] ) : false;
var controlIsDown:Boolean = x.@control.length() ? KeyManager.keyIsDown( KeyManager [ x.@control.toString().toUpperCase() ] ) : false;
var customIsDown:Boolean =  x.@code.length() ? KeyManager.keyIsDown( KeyManager [ x.@code.toString().toUpperCase() ] ) : false;

我认为 KeyManager 行有点奇怪。我不知道 Flex 或常规 AS3 中的 KeyManager,所以这是自定义代码吗?如果是这样,您可以通过类似方法的方式将匹配的大写键代码放在那里customKeyIsDown(),而不是在这里做所有这些。Shift 和 Control 无论如何都是固定的,所以不需要反向匹配 XML 的值,对吧?

var shiftIsDown:Boolean = x.@shift.length() ? KeyManager.keyIsDown( KeyManager.SHIFT ) : false;
var controlIsDown:Boolean = x.@control.length() ? KeyManager.keyIsDown( KeyManager.CONTROL ) : false;
var customIsDown:Boolean =  x.@code.length() ? KeyManager.customKeyIsDown( x.@code ) : false;

我认为这已经更清楚了,但同样,我不知道 KeyManager 究竟做了什么。在此之后,您仍然有三个变量,它们都是可选的。如果它们都需要排他性,那么您就有 8 种可能的结果。

if ( shiftIsDown && controlIsDown && customIsDown ) {
    // 1
} else if ( shiftIsDown && controlIsDown ) {
    // 2    
} else if ( shiftIsDown && customIsDown ) {
    // 3    
} else if ( shiftIsDown ) {
    // 4    
} else if ( controlIsDown && customIsDown ) {
    // 5    
} else if ( controlIsDown ) {
    // 6    
} else if ( customIsDown ) {
    // 7
} else {
    // 8
}

如果您基于键执行的操作不是排他性的,您可以返回仅执行三个基于键的任务

if ( shiftIsDown ) {
    // 1
}
if ( controlIsDown ) {
    // 2
}
if ( customIsDown ) {
    // 3
}

这有帮助吗?干杯,EP。

于 2011-02-06T11:10:12.303 回答