您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Content Index
Content Page # 15

Recall of RandomBall method overriding

For another example of method overriding and inheritance look back at the unit 2 class RandomBall , (in version 8 of the bouncing ball world) which overrode the move() method to be random:

// RandomBall.java
public class RandomBall extends Ball
{ public RandomBall (int newX, int newY, int newRadius,int newXMotion, int newYMotion) {
    // call the Ball constructor
    super( newX, newY, newRadius, newXMotion,  newYMotion ); }
public void move()
     {
      // 50% chance of chaning X motion
      if( Math.random() > 0.5 )       dx = -dx;       // 50% chance of changing Y motion
      if( Math.random() > 0.5 )       dy = -dy;       // move centre of object       x = x + dx;
      y = y + dy;
      }
  } 
// class RandomBall
Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg