domingo, 29 de septiembre de 2013

Source code: InitializationOrder

Código fuente del programa InitializationOrder

Ver la entrada correspondiente en el siguiente enlace:


Java: creando objetos - Parte 2



import java.io.PrintStream;

public class InitializationOrder {

    //static attribute for counting invocations.
    private static int nextValue= 0;
    //--------------------------------------------------------------------------

    //Prefixes the argument with the current value of nextValue and increases it.
    public static String next(String aString) {
        String str= String.format("%02d%s"
            , InitializationOrder.nextValue++
            , aString
        );

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

    //Helper method.
    public static String makeString(String[] aFormats) {
        StringBuilder buffer= new StringBuilder();
        for (int i= 0; i < aFormats.length; i++) {
            buffer.append( aFormats[i] );
        }

        String str= buffer.toString();
        str= String.format(str);

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

    public static String echo(String aMessage) {
        //This sentence is useful only for tracing purposes. It can be commented.
        System.out.format("echo: %s%n", aMessage);

        return aMessage;
    }
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------

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

        aux= new Super();
        out.format("Using Super implicit (automatic) constructor:%n%s%n", aux);
        out.format("..........%n");

        aux= new Base("FIFTH: Initialized at the end of Base(String).");
        out.format("Using Base(String):%n%s%n", aux);
        out.format("..........%n");

        aux= new Base();
        out.format("Using Base():%n%s%n", aux);
        out.format("..........%n");

        aux= new Base() {

            //Local anonymous class attributes.
            private String localFirst= echo(next("Local class instance attribute assignment."));
            private String localSecond;
            
            //Local anonymous Initialization block.
            {
                this.localSecond= echo(next("Local class initialization block."));
            }
            
            @Override
            public String toString() {
                String str= makeString(new String[]{
                    "\tlocalFirst=" + this.localFirst + "%n",
                    "\tlocalSecond=" + this.localSecond + "%n",
                });
                str= String.format("%s%s", super.toString(), str);

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

        };
        out.format("Using Anonymous Local Class extending Base():%n%s%n", aux);
        out.format("..........%n");

        out.format("%n");
    }
    //--------------------------------------------------------------------------


public static class Super {

    private String superFirst=  echo(next("FIRST: Super instance assignment."));
    private String superSecond= echo(next("SECOND: Super Instance assignment: this one is changed later."));
    
    //Initialization block 1.
    {
        this.superSecond= echo(next("SECOND: Super initialization block 1."));
    }
    //--------------------------------------------------------------------------

    //This class has no explicit constructor.

    @Override
    public String toString() {
        String str= makeString(new String[]{
            "\tsuperFirst=" + this.superFirst + "%n",
            "\tsuperSecond=" + this.superSecond + "%n",
        });
        str= String.format("%s: atributes:%n%s", this.getClass(), str);

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

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


public static class Base extends Super {

    //First of all, all the attributes are initialized with their default value.
    //0 for int, false for boolean, null for object referencies, etc.
    private String basePreinitialized;//In this case, the default value is null.
    private String baseFirst=  echo(next("FIRST: Base instance assignment."));
    private String baseSecond;

    //Initialization block 1.
    {
        this.baseSecond= echo(next("SECOND: Base initialization block 1."));
        this.baseThird= echo(next("SECOND: Base initialization block 1. This one is changed later."));
    }

    //Initialization block 2.
    {
        this.baseThird= echo(next("THIRD: Base initialization block 2."));
    }

    //Initialization block 3.
    {
        this.baseFourth= echo(next("FOURTH: Base initialization block 3. This one is changed later."));
    }

    private String baseThird;
    private String baseFourth= echo(next("FOURTH: Base instance assignment."));

    private String baseFifth= echo(next("FIFTH: Base instance assignment. This one is changed later."));
    //--------------------------------------------------------------------------

    public Base(String aFifth) {
        super();
        
        this.baseFifth= aFifth;
        echo("End of Base(String)");
    }
    //--------------------------------------------------------------------------

    public Base() {
        this( next("Modified at the end of Base().") );

        this.baseFifth= next("FIFTH: " + this.baseFifth);
        echo("End of Base()");
    }
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------

    @Override
    public String toString() {
        String str= makeString(new String[]{
            "\tbasePreinitialized=" + this.basePreinitialized + "%n",
            "\tbaseFirst=" + this.baseFirst + "%n",
            "\tbaseSecond=" + this.baseSecond + "%n",
            "\tbaseThird=" + this.baseThird + "%n",
            "\tbaseFourth=" + this.baseFourth + "%n",
            "\tbaseFifth=" + this.baseFifth + "%n",
        });
        str= String.format("%s%s", super.toString(), str);

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

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

}//End of InitializationOrder.

No hay comentarios:

Publicar un comentario