domingo, 27 de octubre de 2013

Source code: OverridableMethodTest

Código fuente del programa OverridableMethodTest

Ver la entrada correspondiente en el siguiente enlace:


Java: Overridable method call in constructor



package pruebas;

import java.io.PrintStream;

public class OverridableMethodTest {

    //Example.
    public static void main(String[] args) {
        final PrintStream out= System.out;

        Super one= new One();
        out.format("Instance of One. Expected value 1 =>%s%n", one);

        Super two= new Two();
        out.format("Instance of Two. Expected value 2 =>%s%n", two);

        out.format("After invoking setValue(...):%n");
        one.setValue(10);
        out.format("Instance of One. Expected value 10 =>%s%n", one);
        two.setValue(20);
        out.format("Instance of Two. Expected value 20 =>%s%n", two);
    }
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    

abstract public static class Super {

    private int value= 999;
    //--------------------------------------------------------------------------

    public Super() {
        super();

        int aux= this.getInitialValue();
        this.setValue(aux);
    }
    //--------------------------------------------------------------------------

    public int getValue() {
        return this.value;
    }
    public void setValue(int aValue) {
        this.value= aValue;
    }
    //--------------------------------------------------------------------------

    abstract public int getInitialValue();
    //--------------------------------------------------------------------------

    @Override
    public String toString() {
        String str= String.format("Class<%s>: Value<%s> InitialValue<%s>"
            , this.getClass().getSimpleName()
            , this.getValue()
            , this.getInitialValue()
        );

        return str;
    }
    //--------------------------------------------------------------------------

}
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    

public static class One extends Super {

    private int initialValue= 1;
    //--------------------------------------------------------------------------

    public One() {
        super();
    }
    //--------------------------------------------------------------------------

    @Override
    public int getInitialValue() {
        return this.initialValue;
    }
    //--------------------------------------------------------------------------

}
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------


public static class Two extends Super {

    private int initialValue= 2;
    //--------------------------------------------------------------------------

    public Two() {
        super();
    }
    //--------------------------------------------------------------------------

    @Override
    public int getInitialValue() {
        return this.initialValue;
    }
    //--------------------------------------------------------------------------

}
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------

}

No hay comentarios:

Publicar un comentario