AS3:测试可选参数和组合
我正在重新审视一些过滤 XML 的旧代码,但这可以很容易地应用于方法的参数(我使用它的方式,本质上就是这样)。我觉得这是一个我经常遇到的问题,但不知道解决这个问题的好方法。
所以问题是我有 3 个参数。它们都是可选的。我想看看哪些是存在的,并测试它们的值是否基于哪些是存在的(根据可能性排序):
var shiftDown : Boolean = false;
var controlDown : Boolean = false;
if ( "@shift" in x )
{
shiftDown = Global.stringToBoolean( [email protected]() );
}
if ( "@control" in x )
{
controlDown = Global.stringToBoolean( [email protected]() );
}
if ( "@code" in x && "@shift" in x && "@control" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ [email protected]().toUpperCase() ] ) && ( KeyManager.shiftKey == shiftDown ) && ( KeyManager.controlKey == controlDown ) )
{
...
}
}
else if ( "@code" in x && "@shift" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ [email protected]().toUpperCase() ] ) && ( KeyManager.shiftKey == shiftDown ) )
{
...
}
}
else if ( "@code" in x && "@control" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ [email protected]().toUpperCase() ] ) && ( KeyManager.controlKey == controlDown ) )
{
...
}
}
else if ( "@code" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ [email protected]().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 语句顺序错误。改变了这一点。
这可以一概而论。为了清楚起见,我只是包含我的代码。如果一般问题仍然不清楚,我的印象是:
测试唯一可选参数的所有组合的最干净/最有效的方法是什么?
I'm revisiting some old code that filters XML, but this could easily apply to the parameters of a method (the way I'm using it, it essentially is). This is a problem I feel like I run into a lot and don't know a good way around this.
So the problem is that I've got 3 arguments. They're all optional. I want to see which ones are presents and test if their values based on which ones are present (sorted according to likelihood):
var shiftDown : Boolean = false;
var controlDown : Boolean = false;
if ( "@shift" in x )
{
shiftDown = Global.stringToBoolean( [email protected]() );
}
if ( "@control" in x )
{
controlDown = Global.stringToBoolean( [email protected]() );
}
if ( "@code" in x && "@shift" in x && "@control" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ [email protected]().toUpperCase() ] ) && ( KeyManager.shiftKey == shiftDown ) && ( KeyManager.controlKey == controlDown ) )
{
...
}
}
else if ( "@code" in x && "@shift" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ [email protected]().toUpperCase() ] ) && ( KeyManager.shiftKey == shiftDown ) )
{
...
}
}
else if ( "@code" in x && "@control" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ [email protected]().toUpperCase() ] ) && ( KeyManager.controlKey == controlDown ) )
{
...
}
}
else if ( "@code" in x )
{
if ( KeyManager.keyIsDown( KeyManager[ [email protected]().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 ) )
{
...
}
}
I feel like there has to be a shorter way to write this with so much repetition in it's current form. Can someone suggest of a cleaner and more efficient way to write this?
Thanks for your thoughts.
EDIT: The if statement order was wrong. Changed that.
This can be generalized. I'm just including my code for clarity. If the general question is still unclear, which I get the impression it is:
What is the cleanest / most efficient way to test all combinations of exclusively optional arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将在这里做出一些假设。从您的示例来看,我认为您想要根据按键执行任务。 XML 似乎包含某种首选项,无论您想要启用还是禁用某些键。您有两个确定的键:Shift 和 Control,以及一个基于键代码的通配符键。如果这些假设是正确的,您应该能够通过将偏好测试和实际的键测试结合在一行上来缩短时间。
我认为 KeyManager 的线条有点奇怪。我不知道 Flex 或常规 AS3 中有 KeyManager,那么这是自定义代码吗?如果是这样,您可以通过诸如
customKeyIsDown()
方法之类的方法将大写按键代码匹配放在那里,而不是在此处执行所有操作。无论如何,Shift 和 Control 都是固定的,因此不需要反向匹配 XML 的值,对吧?我认为这已经有点清楚了,但同样,我不知道 KeyManager 到底是做什么的。之后你仍然有三个变量,它们都是可选的。如果它们都需要是排他性的,那么您就会有 8 种可能的结果。
如果您基于按键执行的操作不是排他性的,那么您可以返回到仅执行基于三个按键的任务,
这有帮助吗?
干杯,
EP。
I'm going to make a few assumptions here. From your example, I take it you want to perform tasks based on key presses. The XML seems to contain some kind of preferences, whether you want to enable or disable certain keys. You have two determined keys, shift and control, and one wildcard key based on the key code. If these assumptions are correct, you should be able to shorten things by combining the preference test and the actual key test on one line.
I think the KeyManager lines are a bit strange. I'm not aware of a KeyManager in either Flex or regular AS3, so is this custom code? If so, you could put the upper case key code matching in there via something like a
customKeyIsDown()
method, instead of doing all that in here. Shift and Control are fixed anyway, so no need to reverse match the XML's value, right?I think this is already be a bit clearer, but again, I don't know what the KeyManager does exactly. After this you still have three variables, and they're all optional. If they all need to be exclusive, that leaves you with 8 possible outcomes.
If what you're doing based on the keys is not exclusive though, you could go back to just performing the three key based tasks
Does this help?
Cheers,
EP.