michelle wu wrote:Thanks Jeff, that is quite straight forward.
Actually the super class A here contains quite a lot of fields, is it possible to do it in a CLONE way rather than copy each fields one by one?
No, afraid not, at least not exactly as you describe. The clone() method is inappropriate for 2 reasons: 1) It always produces a new object. It sounds like you want to set the fields on an existing object. 2) The only way to get the exact copy using clone() without copying the fields yourself is to call super.clone(), but that produces an exact copy of the entire original object. So if you started with a B, you could only use this approach to make another B, not to make a C.
However, what you could do is to have a separate class, X, that encapsulates the common data, and clone that. Even without this cloning issue, that's probably a better design anyway.
posted 8 years ago
Thanks very much Jeff, start refactoring my Class A now