martes, 15 de julio de 2014

Introducción a POO (objeto)



Definición:

Los objetos son la base para un lenguaje POO. Todo lo real se lo lleva a la programación.
Un objeto en Java puede ser cualquier objeto físico (como casa, perro) o conceptual en la vida real (evento, tarea, etc...). Se lo denomina objeto de software.
Los objetos en la vida real tienen dos componentes:

  1. Estado o características representados en un objeto de software con campos o variables (fields).
  2. Comportamiento que son las acciones que realizan los objetos. En un objeto de software son representados como métodos o funciones (methods)
Ejemplo

/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

public class CreateObjectDemo {

    public static void main(String[] args) {
                
        //Declara y crea un objeto Point
        //y dos objetos Rectangle.
        Point originOne = new Point(23, 94);
        Rectangle rectOne = new Rectangle(originOne, 100, 200);
        Rectangle rectTwo = new Rectangle(50, 100);
                
        //muestra la anchura, altura y área de rectOne
        System.out.println("Anchura de rectOne: " +
                rectOne.width);
        System.out.println("Altura de  rectOne: " +
                rectOne.height);
        System.out.println("Área de rectOne: " + rectOne.getArea());
                
        //fijar la posición de rectTwo's
        rectTwo.origin = originOne;
                
        //mostrar la posición de rectTwo's
        System.out.println("Posición X de rectTwo: "
                + rectTwo.origin.x);
        System.out.println("Posición Y de rectTwo: "
                + rectTwo.origin.y);
                
        //mueve rectTwo y muestra su nueva posición
        rectTwo.move(40, 72);
        System.out.println("Posición X de rectTwo: "
                + rectTwo.origin.x);
        System.out.println("Posición Y de rectTwo: "
                + rectTwo.origin.y);
    }
}
 
Este programa crea, manipula y muestra información acerca de varios objetos. Esta es su salida:
Anchura de rectOne: 100
Altura de rectOne: 200
Área de rectOne: 20000
Posición X de rectTwo: 23
Posición Y de rectTwo: 94
Posición X de rectTwo: 40
Posición Y de rectTwo: 72

No hay comentarios:

Publicar un comentario

 

Sample text

Sample Text

Sample Text

 
Blogger Templates