This shows you the differences between two versions of the page.
|
java:class_constructors [2020/02/08 06:25] gthanos [Κατασκευαστές της κλάσης] |
java:class_constructors [2022/02/23 10:17] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Κατασκευαστές της κλάσης ====== | ||
| - | |||
| - | Οι κατασκευαστές μιας κλάσης είναι ειδικές μέθοδοι με τα εξής χαρακτηριστικά: | ||
| - | - έχουν προσδιοριστή πρόσβασης **public**. | ||
| - | - δεν έχουν επιστρεφόμενο τύπο και | ||
| - | - το όνομα τους ταυτίζεται με το όνομα της κλάσης. | ||
| - | |||
| - | Για παράδειγμα, | ||
| - | |||
| - | <code java> | ||
| - | code java Rectangle.java> | ||
| - | public class Rectangle { | ||
| - | int width; | ||
| - | int height; | ||
| - | Point origin; | ||
| - | | ||
| - | public Rectangle(int initWidth, int initHeight, Point initOrigin) { | ||
| - | width = initWidth; | ||
| - | height = initHeight; | ||
| - | origin = initOrigin; | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Ο ρόλος του κατασκευαστή είναι να δεσμεύσει την απαραίτητη μνήμη για την δημιουργία του αντικειμένου και να αρχικοποιήσει τα πεδία της κλάσης με βάση τις αρχικές παραμέτρους με τις οποίες καλείται. Η κλήση του κατασκευαστή γίνεται πάντα με χρήση του τελεστή **new** και θα τη δούμε σε επόμενη ενότητα. | ||
| - | |||
| - | <WRAP tip 80% center round> | ||
| - | Κάθε κλάση έχει τουλάχιστον ένα κατασκευαστή. Εάν δεν δηλώσετε κατασκευαστή σε κάποια κλάση ο // | ||
| - | </ | ||
| - | |||
| - | ===== Default κατασκευαστής ===== | ||
| - | |||
| - | Στο προηγούμενο παράδειγμα της κλάσης **Rectangle** εάν υποθέσουμε ότι αυτή δεν έχει κατασκευαστή ο // | ||
| - | |||
| - | <code java Rectangle.java> | ||
| - | public class Rectangle { | ||
| - | int width; | ||
| - | int height; | ||
| - | Point origin; | ||
| - | | ||
| - | public Rectangle() { | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | , ο οποίος με τη σειρά του αρχικοποιεί όλες τις τιμές σε μηδέν για τους βασικούς (// | ||
| - | <code java> | ||
| - | public Rectangle() { | ||
| - | width = 0; | ||
| - | height = 0; | ||
| - | origin = null; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | με βάση τους [[java: | ||
| - | |||
| - | ===== Πολλαπλοί κατασκευαστές σε μία κλάση ===== | ||
| - | |||
| - | Ανάλογη με την υπερφόρτωση μεθόδων είναι και η " | ||
| - | |||
| - | <code java> | ||
| - | public Rectangle(int initWidth, int initHeight, Point initOrigin); | ||
| - | public Rectangle(int initWidth, int initHeight, int xPos, int yPos); | ||
| - | </ | ||
| - | |||
| - | |||
| - | <code java Rectangle.java> | ||
| - | public class Rectangle { | ||
| - | | ||
| - | // the Rectangle class has 3 fields | ||
| - | private int width; | ||
| - | private int height; | ||
| - | private Point origin; | ||
| - | | ||
| - | // the Rectangle class has two constructors | ||
| - | public Rectangle(int initWidth, int initHeight, Point initOrigin) { | ||
| - | width = initWidth; | ||
| - | height = initHeight; | ||
| - | origin = initOrigin; | ||
| - | } | ||
| - | | ||
| - | public Rectangle(int initWidth, int initHeight, int xPos, int yPos) { | ||
| - | width = initWidth; | ||
| - | height = initHeight; | ||
| - | origin = new Point(xPos, | ||
| - | } | ||
| - | | ||
| - | // the Rectangel class has 3 methods | ||
| - | public void setWidth(int newWidth ) { | ||
| - | width = newWidth; | ||
| - | } | ||
| - | | ||
| - | public int getWidth() { | ||
| - | return width; | ||
| - | } | ||
| - | | ||
| - | public void setHeight(int newHeight ) { | ||
| - | height = newHeight; | ||
| - | } | ||
| - | | ||
| - | public int getHeight() { | ||
| - | return height; | ||
| - | } | ||
| - | | ||
| - | public void setOrigin(Point newOrigin) { | ||
| - | origin = newOrigin; | ||
| - | } | ||
| - | | ||
| - | public Point getOrigin() { | ||
| - | return origin; | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |Προηγούμενο: | ||