java:inheritance
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:inheritance [2021/03/05 17:26] – gthanos | java:inheritance [2022/03/11 05:21] (current) – gthanos | ||
---|---|---|---|
Line 10: | Line 10: | ||
<code java BasicRectangle.java> | <code java BasicRectangle.java> | ||
- | class BasicRectangle { | + | public |
| | ||
int width; | int width; | ||
int height; | int height; | ||
- | public | + | public |
width = initWidth; | width = initWidth; | ||
height = initHeight; | height = initHeight; | ||
} | } | ||
- | void setWidth(int newWidth ) { | + | |
width = newWidth; | width = newWidth; | ||
} | } | ||
| | ||
- | void setHeight(int newHeight ) { | + | |
height = newHeight; | height = newHeight; | ||
} | } | ||
| | ||
- | int getWidth() { | + | |
return width; | return width; | ||
} | } | ||
| | ||
- | int getHeight() { | + | |
return height; | return height; | ||
} | } | ||
| | ||
- | | + | |
- | return width * height; | + | return |
} | } | ||
- | |||
} | } | ||
</ | </ | ||
Line 76: | Line 75: | ||
<code java Rectangle.java> | <code java Rectangle.java> | ||
- | class Rectangle extends BasicRectangle{ | + | public |
| | ||
Point origin; | Point origin; | ||
Line 90: | Line 89: | ||
} | } | ||
| | ||
- | void setOrigin(Point newOrigin) { | + | |
origin = newOrigin; | origin = newOrigin; | ||
} | } | ||
| | ||
- | Point getOrigin() { | + | |
return origin; | return origin; | ||
+ | } | ||
+ | | ||
+ | int area() { | ||
+ | return width * height; | ||
} | } | ||
// Move rectangle origin by dx,dy | // Move rectangle origin by dx,dy | ||
- | void moveOrigin(int dx, int dy) { | + | |
origin.setX( origin.getX() + dx ); | origin.setX( origin.getX() + dx ); | ||
origin.setY( origin.getY() + dy ); | origin.setY( origin.getY() + dy ); | ||
+ | } | ||
+ | | ||
+ | public String toString() { | ||
+ | String str = origin.toString() + " "; | ||
+ | str = str + super.toString(); | ||
+ | return str; | ||
} | } | ||
} | } |
java/inheritance.1614965202.txt.gz · Last modified: 2021/03/05 17:26 by gthanos