Ho le seguenti due classi:costruttore di copia in Child ma non Parent
public class User {
public Integer userId;
// ...another 50-60 fields
}
public class SuperUser extends User {
}
mi piacerebbe avere un costruttore in SuperUser che prende un oggetto di tipo utente e crea un oggetto di tipo SuperUser. Per esempio:
public SuperUser(User theUser) {
// not legal -> but I am looking for a one-liner to initialize this with values from theUser
this = theUser;
}
Se l'oggetto utente manca il costruttore utente (User existingUser), non v'è alcun modo automatico per inizializzare l'oggetto SuperUser con tutti i campi dalla oggetto utente esistente? Sto cercando di evitare 50 linee di:
public SuperUser(User theUser) {
this.firstName = theUser.getFirstName();
this.lastName = theUser.getLastName();
// and so on....
}
Se ciò non può essere fatto, c'è un refactoring come "Crea copia Constructor?"
Grazie!
Non credo che stai rispondendo alla domanda;) – Bozho
Ma, se non ricordo male, non ci sono veri costruttori di copia dedicati in Java, così come può li usare? – AndrejaKo
Li implementate manualmente. – royal