是否可以循环设置器和获取器?
我相当有信心这不可能起作用,但我还是想问一下,以防万一我错了:
我多次听说,每当你在一批中有一定数量的非常相似的代码行时,你应该总是循环遍历它们。
所以说我有类似下面的东西。
setPos1(getCard1());
setPos2(getCard2());
setPos3(getCard3());
setPos4(getCard4());
setPos5(getCard5());
setPos6(getCard6());
setPos7(getCard7());
setPos8(getCard8());
setPos9(getCard9());
setPos10(getCard10());
setPos11(getCard11());
setPos12(getCard12());
没有办法减少代码行数,如下所示,对吗?
for (i = 0; i < 12; i++) {
setPos + i(getCard + i)());
}
我确信以前有人问过这个问题,但谷歌和 SO 搜索都没有给出否定的证据。
感谢您快速确认这一点!
I'm fairly confident that there's no way this could work, but I wanted to ask anyway just in case I'm wrong:
I've heard many times that whenever you have a certain number of lines of very similar code in one batch, you should always loop through them.
So say I have something like the following.
setPos1(getCard1());
setPos2(getCard2());
setPos3(getCard3());
setPos4(getCard4());
setPos5(getCard5());
setPos6(getCard6());
setPos7(getCard7());
setPos8(getCard8());
setPos9(getCard9());
setPos10(getCard10());
setPos11(getCard11());
setPos12(getCard12());
There is no way to cut down on lines of code as, e.g., below, right?
for (i = 0; i < 12; i++) {
setPos + i(getCard + i)());
}
I'm sure this will have been asked before somewhere, but neither Google nor SO Search turned up with a negative proof.
Thanks for quickly confirming this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果没有反射,就没有办法专门在 Java 中做到这一点,而且我认为这是不值得的。这看起来更像是一个提示,表明您应该重构 getcard 函数以采用整数参数。然后你就可以循环了。
No way to do that specifically in Java without reflection, and I don't think it would be worth it. This looks more like a cue that you should refactor your getcard function to take an integer argument. Then you could loop.
这是一个简单的代码片段,展示了如何使用反射循环访问某个对象的 getter 来检查返回值是否为 null:
就像其他人所说的那样,这可能不是一个优雅的实现。这只是为了完整性。
This is a simple snippet that shows how to loop through the getters of a certain object to check if the returned values are null, using reflection:
Like the others stated, probably it's not an elegant implementation. It's just for the sake of completeness.
你可以通过反射来做到这一点,但这会很麻烦。更好的方法可能是创建通用的 setPos() 和 getCard() 方法,您可以将当前项目的索引传递到其中。
You could do it via reflection, but it would be cumbersome. A better approach might be to make generic setPos() and getCard() methods into which you could pass the index of the current item.
您需要放弃 getter/setter 对,并使用
List
来存储您的对象,而不是尝试将所有内容填充到一个 God 对象中。这是一个人为的例子:
}
You need to ditch the getter/setter pairs, and use a
List
to store your objects rather then trying to stuff everything into one God object.Here's a contrived example:
}
您无法动态构造方法名称然后调用它(无需反射)。即使有反射,它也会有点脆弱。
一种选择是将所有这些操作集中到一个方法中,例如 setAllPositions ,然后只调用该方法。
或者,您可以有一个位置数组,然后循环遍历该数组,在每个索引处设置值。
Card[] cardsAtPosition = new Card[12];
然后类似
You can't dynamically construct a method name and then invoke it (without reflection). Even with reflection it would be a bit brittle.
One option is to lump all those operations into one method like
setAllPositions
and just call that method.Alternatively, you could have an array of positions, and then just loop over the array, setting the value at each index.
Card[] cardsAtPosition = new Card[12];
and then something like
对于您的示例案例,反射将是您的唯一选择。
Reflection would be your only option for your example case.