.


:




:

































 

 

 

 


.




, . ? . :

, -.

:

, -.

, ( new, ), ( ).

, , , -. . , , , , moveTo, dx dy . ( ). FiguresUtil ( Utilities , ), moveFigureBy ( ).

 

public class FiguresUtil{

 

public static void moveFigureBy(Figure figure,int dx, int dy){

figure.moveTo(figure.x+dx, figure.y+dy);

}

}

 

figure , . , , , : , Dot. , Circle. , FilledCircle. , FilledCircle Figure, Dot Circle.

:

 

 

Figure figure;

java.awt.Graphics g=jPanel1.getGraphics();

 

//

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

if(jRadioButton1.isSelected())

figure=new Dot(g,jPanel1.getBackground());

if(jRadioButton2.isSelected())

figure=new Circle(g,jPanel1.getBackground());

if(jRadioButton3.isSelected())

figure=new FilledCircle(g,jPanel1.getBackground(),20,

java.awt.Color.BLUE);

figure.show();

}

 

//

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

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

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

FiguresUtil.moveFigureBy(figure,dx,dy);

}

 

, , . - .

FiguresUtil.moveFigureBy(figure,dx,dy);

 

:

FiguresUtil.moveFigureBy(

figure,

Integer.parseInt(jTextField1.getText()),

Integer.parseInt(jTextField2.getText())

);

 

( 8 !), , .

 

: . ,

Figure figure;

Circle circle;

figure =new Circle (); //

circle= figure; - ! . , figure Circle , .

, -, . , :

Figure figure;

Circle circle;

Dot dot;

figure =new Circle (); //

circle= (Circle)figure; // !

dot=(Dot) figure; // !

 

, , . . , , . Java , . , , . .

, .

, - , , , , , . , (Circle)figure , figure Dot, Circle. (typecast).

, :

if(figure instanceof Circle)

System.out.println("figure instanceof Circle");

 

. .class .

:

Class c=Circle.class;

. , newInstance():

Circle circle=(Circle)c.newInstance();

 

:

if(figure.getClass()==Circle.class)

circle= (Circle)figure;

;

, , . isInstance , figure , c:

if(c.isInstance(figure))

System.out.println("figure isInstance of Circle");

.

 

.

 

-, , , .

-, .

-, .

 

, , (, Extract method), . .

- .

, NetBeans , . , Projects Navigator. Refactor/Rename Next>.

 

. 1

 

. 2

 

Preview All Changes ( ) , , Output (), , . , . Do Refactoring ( ) . , , , , . NetBeans , .

 

. 3

 

: Output Do Refactoring. . ( 2) Preview all Changes , Next> .

, (, undo). ( Undo Edit/Undo), Refactor/Undo. . Redo.





:


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


:

:

, .
==> ...

1371 - | 1223 -


© 2015-2024 lektsii.org - -

: 0.02 .