Thursday 15 March 2012

java - Giving fields values without stating the names of the fields -


There is a class in my program that has a large number of fields, let me create a new instance of this class with two existing examples already Should be able to. It randomly selects which fields are from two previous instances.

Obviously I can do a bunch of these:

  if (random.nextBoolean ()) This.value = a.value; Other: this.value = b.value;   

However, I have always been amazed in some ways found in classman.class. I have tried to research this before, but I have never got any luck. So let me tell you what I would like to do:

 for  (int i = 0; i & lt; specimen.claus.getdclaimerfield) Length; i ++) {if ( Random.nextBoolean ()) This.fields [i] = a.fields [i]; And this. Field [ii] = B.field [I];   

It is my understanding that with reflection this is not possible, is there any other way?

field and then holds the value of the field more.

To get the value of the field, for example you get the set / method .

  if (random.nextBoolean ()) this.fields [i] .set (a.fields [i] .get ()); And these. Field [i] .set (b. Field [i] .get ());    

No comments:

Post a Comment