jueves
abr092015
Más ejemplos sobre constructores
¿Compilaría y ejecutaría el siguiente código? Si no compila, ¿por qué razón? Si ejecuta, ¿cuál sería la salida?
Clase constructores1.Pruebas
package constructores1;
public class Pruebas {
private String s = "publico";
protected Pruebas() {
this.s = "protected";
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Pruebas [s=" + s + "]";
}
}
Clase constructores2.Pruebas
package constructores2;
public class Pruebas {
private String s = "publico";
protected Pruebas() {
this.s = "protected";
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Pruebas4 [s=" + s + "]";
}
}
Clase Principal
package constructores;
public class Principal {
public static void main(String args[]){
constructores1.Pruebas pruebas1 = new constructores1.Pruebas();
System.out.println(pruebas1.toString());
constructores1.Pruebas pruebas2 = new constructores2.Pruebas();
System.out.println(pruebas2.toString());
}
}
tagged
constructores in
ocjp
constructores in
ocjp 