80.继承或子类
|
![BACKWARD](backward.gif) ![FORWARD](forward.gif) |
- 我们可以定义一个新的类 mRectangle, 它从 mPoint 中扩充而来, 其中来自mPoint
的(x,y,color) 是左下角位置和颜色, 我们要增加宽度和高度
- public class mRectangle extends mPoint {
- int w, h;
- public mRectangle(int _x, int _y, int _w, int _h) {
- super(_x, _y); /* call mPoint's constructor */
- w = _w;
- h = _h;
- } /* End mRectangle Constructor */
- /* overwrite the mPoint's checkBoundry method */
- public void checkBoundry(Rectangle rect) {
- int nx = x+dx;
- int ny = y+dy;
- if ( (nx < rect.x) || (nx+w >= rect.x+rect.width) ) dx = -dx;
- if ( (ny < rect.y) || (ny+h >= rect.y+rect.height) ) dy =
-dy;
- }
- public void paint(Graphics g) { /* Override mPoint Method */
- g.setColor(color);
- g.drawRect(x, y, w, h);
- }
}
Copyright: NPACT |
![BACKWARD](backward.gif) ![FORWARD](forward.gif) |