This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
java:objects [2015/09/18 13:40] gthanos |
java:objects [2016/01/22 15:10] gthanos |
||
---|---|---|---|
Line 3: | Line 3: | ||
Μέχρι τώρα αναφέραμε στην [[ oop:introduction | "Εισαγωγή στον Αντικειμενοστραφή Προγραμματισμό" ]] ότι η κλάση είναι το βασικό σχέδιο μέσα από το οποίο δημιουργούνται επιμέρους αντικείμενα που φέρουν τα χαρακτηριστικά της κλάσης. Επίσης, δείξαμε πως ορίζουμε μία κλάση μέσα από παραδείγματα, αλλά δεν δείξαμε πως δημιουργούμε αντικείμενα από τις κλάσεις που ορίσαμε. | Μέχρι τώρα αναφέραμε στην [[ oop:introduction | "Εισαγωγή στον Αντικειμενοστραφή Προγραμματισμό" ]] ότι η κλάση είναι το βασικό σχέδιο μέσα από το οποίο δημιουργούνται επιμέρους αντικείμενα που φέρουν τα χαρακτηριστικά της κλάσης. Επίσης, δείξαμε πως ορίζουμε μία κλάση μέσα από παραδείγματα, αλλά δεν δείξαμε πως δημιουργούμε αντικείμενα από τις κλάσεις που ορίσαμε. | ||
- | Παρακάτω δίνεται ένα πρόγραμμα που δημιουργεί συγκεκριμένα αντικείμενα και εκτυπώνει τα αποτελέσματα στην κονσόλα. | + | Παρακάτω δίνεται ένα πρόγραμμα που δημιουργεί συγκεκριμένα αντικείμενα του τύπου **Point** και **Rectangle** και εκτυπώνει τα αποτελέσματα στην κονσόλα. |
<code java Point.java> | <code java Point.java> | ||
public class Point { | public class Point { | ||
- | private int xCoord, yCoord; | + | private int x; |
- | | + | private int y; |
- | public Point(int x, int y) { | + | |
- | xCoord = x; | + | public Point(int xPos, int yPos) { |
- | yCoord = y; | + | x = xPos; |
- | } | + | y = yPos; |
- | public void setX(int x) { | + | } |
- | xCoord = x; | + | |
- | } | + | public int getX() { |
- | public void setY(int y) { | + | return x; |
- | yCoord = y; | + | } |
- | } | + | |
- | public int getX() { | + | public void setX(int xPos) { |
- | return xCoord; | + | x = xPos; |
- | } | + | } |
- | public int getY() { | + | |
- | return yCoord; | + | public int getY() { |
- | } | + | return y; |
+ | } | ||
+ | |||
+ | public void setY(int yPos) { | ||
+ | y = yPos; | ||
+ | } | ||
} | } | ||
</code> | </code> | ||
Line 30: | Line 35: | ||
<code java Rectangle.java> | <code java Rectangle.java> | ||
public class Rectangle { | public class Rectangle { | ||
- | private int width, height; | + | |
- | private Point origin; | + | // the Rectangle class has 3 fields |
- | | + | private int width; |
- | public Rectangle(Point p, int w, int h) { | + | private int height; |
- | origin = p; | + | private Point origin; |
- | width = w; | + | |
- | height = h; | + | // the Rectangle class has one constructor |
- | } | + | public Rectangle(int setWidth, int setHeight, Point o) { |
- | | + | width = setWidth; |
- | public Rectangle(int w, int h) { | + | height = setHeight; |
- | width = w; | + | origin = o; |
- | height = h; | + | } |
- | } | + | |
- | | + | public Rectangle(int setWidth, int setHeight) { |
- | public void setOrigin(Point p) { | + | width = setWidth; |
- | origin = p; | + | height = setHeight; |
- | } | + | } |
- | | + | |
- | public void setHeight(int h) { | + | // the Rectangel class has 3 methods |
- | height = h; | + | public void setWidth(int newWidth ) { |
- | } | + | width = newWidth; |
- | | + | } |
- | public void setWidth(int w) { | + | |
- | width = w; | + | public int getWidth() { |
- | } | + | return width; |
- | | + | } |
- | public Point getOrigin() { | + | |
- | return origin; | + | public void setHeight(int newHeight ) { |
- | } | + | height = newHeight; |
- | | + | } |
- | public int getHeight() { | + | |
- | return height; | + | public int getHeight() { |
- | } | + | return height; |
- | | + | } |
- | public int getWidth() { | + | |
- | return width; | + | public void setOrigin(Point o) { |
- | } | + | origin = o; |
- | | + | } |
- | public int getArea() { | + | |
+ | public Point getOrigin() { | ||
+ | return origin; | ||
+ | } | ||
+ | |||
+ | public int getArea() { | ||
return width * height; | return width * height; | ||
- | } | + | } |
- | + | ||
- | // Move rectangle origin by x,y | + | |
- | public void move(int x, int y) { | + | |
- | origin.setX( origin.getX() + x ); | + | |
- | origin.setY( origin.getY() + y ); | + | |
- | } | + | |
+ | // Move rectangle origin by x,y | ||
+ | public void move(int dx, int dy) { | ||
+ | origin.setX( origin.getX() + dx ); | ||
+ | origin.setY( origin.getY() + dy ); | ||
+ | } | ||
} | } | ||
</code> | </code> | ||
Line 88: | Line 97: | ||
// Declare and create a point object and two rectangle objects. | // Declare and create a point object and two rectangle objects. | ||
Point originOne = new Point(23, 94); | Point originOne = new Point(23, 94); | ||
- | Rectangle rectOne = new Rectangle(originOne, 100, 200); | + | Rectangle rectOne = new Rectangle(100, 200, originOne); |
Rectangle rectTwo = new Rectangle(50, 100); | Rectangle rectTwo = new Rectangle(50, 100); | ||