Is it possible to set loops and getters?

I'm pretty sure that this cannot work in any way, but I still wanted to ask just in case, if I am mistaken:

I have heard many times that whenever you have a certain number of lines of very similar code in one batch, you should always scroll through them.

So to speak, 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 lines of code, for example, at the bottom, on the right?

 for (i = 0; i < 12; i++) { setPos + i(getCard + i)()); } 

I am sure that this will be asked earlier, but neither Google nor SO Search turned out to be with negative evidence.

Thanks for the quick confirmation of this!

+4
source share
6 answers

It is impossible to do this specifically in Java without reflection, and I don’t think it was worth it. This is more like a link where you have to reorganize your getcard function to accept an integer argument. Then you can execute the loop.

+10
source

You could do it through reflection, but that would be cumbersome. A better approach would be to create common setPos () and getCard () methods to which you could pass the index of the current element.

+3
source

You need to align getter / setter pairs and use List to store your objects, and then try to stuff everything onto one God object.

Here is a contrived example:

 import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Foo { public static class Card { int val; public Card(int val) { this.val = val; } public int getVal() { return val; } } public static class Position { int value; public Position(Card card) { this.value = card.getVal(); } } public static void main(String[] args) { List<Card> cards = new ArrayList<Card>(Arrays.asList(new Card(1), new Card(2), new Card(3))); List<Position> positions = new ArrayList<Position>(); for (Card card : cards) { positions.add(new Position(card)); } } 

}

+2
source

This is a simple snippet that shows how to iterate over the recipients of a specific object to check if the return values ​​are null using reflection:

 for (Method m : myObj.getClass().getMethods()) { // The getter should start with "get" // I ignore getClass() method because it never returns null if (m.getName().startsWith("get") && !m.getName().equals("getClass")) { // These getters have no arguments if (m.invoke(myObj) == null) { // Do something } } } 

Like everyone else, perhaps this is not an elegant implementation. It’s just for the sake of completeness.

+2
source

You cannot dynamically construct a method name and then call it (without reflection). Even with reflection it would be a little fragile.

One option is to combine all these operations into one method, for example, setAllPositions , and simply call this method.

Alternatively, you can have an array of positions, and then just loop over the array, setting a value for each index.

Card[] cardsAtPosition = new Card[12];

and then something like

 public void setCardsAtEachPosition(Card[] valuesToSet) { // check to make sure valuesToSet has the required number of cards for (i = 0; i < cardsAtPosition.length; i++) { cardsAtPosition[i] = valuesToSet[i]; } } 
+1
source

Reflection will be your only option for your example.

0
source

All Articles