java - Moving all fields from one object into another -


can somehow move field values 1 object without using reflection? so, want this:

public class betterthing extends thing implements ibetterobject {     public betterthing(thing t) {         super();         t.evolve(this);     } } 

so, evolve method evolve 1 class another. argument <? extends <? extends t>> t class of object, you're calling evolve on.

i know can reflection, reflection hurts performance. in case thing class in external api, , there's no method copy required fields object.

as @olivercharlesworth points out, can't done directly. either have resort reflection (though wouldn't recommend it!) or series of field-by-field assignments.

another option though, switch inheritance composition:

public class betterthing implements ibetterobject {     thing delegate;      public betterthing(thing t) {         this.delegate = t;     }      // thing methods (delegate methods)     string thingmethodone() {         return delegate.thingmethodone();     }      // betterthing methods } 

this commonly referred decorator pattern.

see: prefer composition on inheritance?


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -