.


:




:

































 

 

 

 


. .




, , / . (ancestors), (descendants) . - , -, (inheritance) .

C++ Java (superclasses) (subclasses). , , .

- , class , extends (), - ( ). , , Object. - , .

Java extends , , , , . ( , ).

. , , . , , - . -. , , .

, :

- public , () . , - .

- abstract , , , (). , - .

- final , , (final), .

, - :

 

class extends {

;

- , ,

}

 

, (interfaces). .

. , Figure - - , Dot , Circle - Dot ( ). , .

Figure , , , . show () hide () . , , .

 

public abstract class Figure { //

 

int x=0;

int y=0;

java.awt.Color color;

java.awt.Graphics graphics;

java.awt.Color bgColor;

public abstract void show(); //

public abstract void hide(); //

 

public void moveTo(int x, int y){

hide();

this.x= x;

this.y= y;

show();

};

}

 

x y , color . java.awt. graphics , . java.awt. x, y color , . bgColor ( background color) . hide , . , . . , , FilledCircle , XOR ( ). setXORMode. .

moveTo , , show hide. , .

, Figure Dot (). Dot Object Figure (), Figure . , Dot (), Figure . Dot (extends) Figure: , show hide, . Figure java.awt . Dot , .

 

package java_gui_example;

 

import java.awt.*;

/**

* @author ..

*/

public class Dot extends Figure{

 

/** Dot */

public Dot(Graphics graphics,Color bgColor) {

this.graphics=graphics;

this.bgColor=bgColor;

}

 

public void show(){

Color oldC=graphics.getColor();

graphics.setColor(Color.BLACK);

graphics.drawLine(x,y,x,y);

graphics.setColor(oldC);

}

public void hide(){

Color oldC=graphics.getColor();

graphics.setColor(bgColor);

graphics.drawLine(x,y,x,y);

graphics.setColor(oldC);

;

}

}

, Dot x, y, graphics moveTo Figure. show hide (override) , , .

Dot(int x, int y, Graphics g) Dot . show hide graphics. show oldC . ( java.awt. Color.BLACK). , , . . , , - . (shared) . - , , .

override () . , (hiding). , - , (, , ). , .

. . , , .

, , . , , (overloading) . . , , .

, . ( - ) background. dot, :

Dot dot=new Dot(jPanel1.getGraphics(),jPanel1.getBackground());

 

- dot show hide:

dot.show();

dot.hide();

/ jTextField1 jTextField2. moveTo, , :

int newX=Integer.parseInt(jTextField1.getText());

int newY=Integer.parseInt(jTextField2.getText());

dot.moveTo(newX,newY);

, .

 

ScalableFigure ( ), Figure. .

 

package java_gui_example;

public abstract class ScalableFigure extends Figure{

int size;

 

public void resize(int size) {

hide();

this.size=size;

show();

}

}

 

ScalableFigure , . show hide.

size (), resize ( ), . , , , size , . , size, . , , , , , - ScalableFigure. , , , .

Circle (), ScalableFigure.

 

package java_gui_example;

import java.awt.*;

 

public class Circle extends ScalableFigure {

 

Circle(Graphics g,Color bgColor, int r){ //

graphics=g;

this.bgColor=bgColor;

size=r;

}

 

public void show(){

Color oldC=graphics.getColor();

graphics.setColor(Color.BLACK);

graphics.drawOval(x,y,size,size);

graphics.setColor(oldC);

}

 

public void hide(){

Color oldC=graphics.getColor();

graphics.setColor(bgColor);

graphics.drawOval(x,y,size,size);

graphics.setColor(oldC);

}

};

Circle size, ScalableFigure. , .

, show hide, , , .

, Circle ScalableFigure , . , Circle ScalableFigure, Figure.

x, y, color, bgColor, graphics moveTo Circle Figure. ScalableFigure size resize.

, , . , . . circle Figure . , , circle - Figure. . , , .

Circle () ScalableFigure, ScalableFigure () Circle. ScalableFigure Circle , Circle ScalableFigure . , Figure ScalableFigure, Circle. Circle ScalableFigure, Figure.

Java Object, Object Figure, ScalableFigure, Circle. Figure.





:


: 2017-02-28; !; : 514 |


:

:

, .
==> ...

1761 - | 1668 -


© 2015-2024 lektsii.org - -

: 0.033 .