ho le seguenti classi:Creare un oggetto classe base da una classe derivata
public class Base{
//fields
public String getStr(){
String str = null;
//Getting str from the fields
return str;
}
}
public class Derived extends Base{
//fields
//some other fileds
public String getStr(){
String str = null;
//Getting str from the fields + some other fields
return str;
}
}
Ora, ho un metodo che ha un parametro di tipo Derived
.
public void makeStr(Derived d){
Base b = null;
//Getting the Base class subobject from d and printing the str
}
ma non posso proprio fare assegnamento come b = d;
e quindi richiamare b.getStr()
perché il metodo d.getStr()
sta per essere chiamato.
Come è possibile creare l'oggetto del tipo Base
dal sottosoggetto Base
dell'oggetto del tipo Derived
? Infatti, creo solo la copia del subwoofer Base
del tipo Derived
.
'str' non è un * campo *, è una * variabile di metodo *. – amit
@amit E? Creiamo str dai campi. Penso che i dettagli della creazione di quella stringa (come invocare i metodi toString() sui campi) non siano importanti qui. –
Ho frainteso il tuo intento, thoguht ti riferivi a 'str' come a un campo stesso – amit