INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

Preview:

DESCRIPTION

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

Citation preview

INSTITUTO TECNOLOGICO SUPERIOR DE LIBRES

INGENIERIA EN SISTEMAS COMPUTACIONALES

LUCERO ARENAS FLORES

CLASE BASE

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.

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}

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

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) {

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);

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) {

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

GRACIAS

Recommended