INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

10
INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES INGENIERIA EN SISTEMAS COMPUTACIONALES LUCERO ARENAS FLORES

description

INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES. INGENIERIA EN SISTEMAS COMPUTACIONALES. LUCERO ARENAS FLORES. CLASE BASE. - PowerPoint PPT Presentation

Transcript of INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

Page 1: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

INGENIERIA EN SISTEMAS COMPUTACIONALES

LUCERO ARENAS FLORES

Page 2: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

CLASE BASE

Page 3: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

Una clase abstracta es una clase que se introduce sólo para que se deriven nuevas clases de ella, no para que se creen objetos con su nombre. Del mismo modo, un método abstracto es un método que se introduce para que sea redefinido en una clase derivada.

Page 4: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

abstract class GraphObj{ int x, y; // La posición central GraphObj(int ix, int iy) { x= ix; y= iy; } // constructor void Move(int dx, int dy) { x+= dx; y+= dy; } abstract void Paint(Graphics g); // Paint es abstracto}

Page 5: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

Esta clase no se puede usar para crear un objeto, por lo que lo siguiente es un error:GraphObj gf= new GraphObj(10,20); // error

Page 6: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

La idea es que sólo se pueden crear objetos de clases derivadas de la clase anterior:class Line extends GraphObj{ // x e y se heredan int ix, iy; GraphObj(int aix, int aiy, int afx, int afy) {

Page 7: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

super((aix+afx)/2, (aiy+afy)/2); ix= aix; iy= aiy; } void Paint(Graphics g) { g.DrawLine(xi,yi,x+(x-xi),y+(y-yi)); } // Move se hereda de GraphObj}

// Ahora sí!Line line= new Line(0,0, 10,20);

Page 8: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

El principio es que se use varias veces la clase abstracta para definir varias otras clases que poseen un conjunto común de métodos: Paint y Move.// Una cajaclass Box extends GraphObj{ int height, width; Box(int lx, int ly, int hx, int hy) {

Page 9: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

super( ... ); // Ejercicio ... } void Paint(Graphics g) { ... // Ejercicio }}

Page 10: INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

GRACIAS