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:this_operator [2016/01/24 17:16] gthanos [Πρόσβαση σε πεδία και μεθόδους των αντικειμένων της κλάσης μέσω του τελεστή this] |
java:this_operator [2020/02/21 12:16] gthanos [Ο τελεστής this] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== | ====== | ||
| - | ===== Πρόσβαση στους | + | Μέσω του τελεστή **this** μπορούμε: |
| + | * να έχουμε πρόσβαση στα πεδία | ||
| + | * κατά τη δημιουργία του αντικειμένου να καλέσουμε ένα κατασκευαστή μέσω ενός άλλου κατασκευαστή. | ||
| - | Ας ξαναγράψουμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα | + | ===== Πρόσβαση σε πεδία και μεθόδους των αντικειμένων της κλάσης μέσω του τελεστή this ===== |
| + | |||
| + | Ας ξαναγράψουμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα, ως εξής | ||
| <code java> | <code java> | ||
| Line 13: | Line 17: | ||
| | | ||
| // the Rectangle class has one constructor | // the Rectangle class has one constructor | ||
| - | public Rectangle(int | + | public Rectangle(int |
| - | width = setWidth; | + | width = width; |
| - | height = setHeight; | + | height = height; |
| - | origin = o; | + | origin = origin; |
| } | } | ||
| - | | + | ... |
| - | public Rectangle(int setWidth, int setHeight) { | + | |
| - | width = setWidth; | + | |
| - | height = setHeight; | + | |
| - | } | + | |
| - | .... | + | |
| } | } | ||
| </ | </ | ||
| - | ως εξής | + | Στον παραπάνω κώδικα οι τοπικές παράμετροι του κατασκευαστή συμπίπτουν με τα ονόματα των μεταβλητών της κλάσης. Σε αυτή την περίπτωση η τοπική μεταβλητή/ |
| <code java> | <code java> | ||
| Line 37: | Line 36: | ||
| | | ||
| // the Rectangle class has one constructor | // the Rectangle class has one constructor | ||
| - | public Rectangle(int | + | public Rectangle(int |
| - | this(setWidth, setHeight); | + | this.width = width; |
| - | origin = o; | + | this.height = height; |
| + | this.origin = origin; | ||
| + | } | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP tip 80% center round> | ||
| + | __O τελεστής this υποδεικνύει το τρέχον αντικείμενο__. Με τον παραπάνω τρόπο μπορούμε να διακρίνουμε μία μεταβλητή του τρέχοντος αντικειμένου | ||
| + | </ | ||
| + | |||
| + | ===== Κλήση ενός κατασκευαστή μέσω άλλου κατασκευαστή με χρήση του τελεστή this ===== | ||
| + | |||
| + | Ας ξαναδούμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα θέλοντας να επιχειρήσουμε να επαναχρησιμοποιήσουμε τμήματα κώδικα ενός κατασκευαστή από έναν άλλο κατασκευαστή. | ||
| + | |||
| + | <code java> | ||
| + | public class Rectangle { | ||
| + | |||
| + | private int width; | ||
| + | private int height; | ||
| + | private Point origin; | ||
| + | |||
| + | public Rectangle(int initWidth, int initHeight, Point initOrigin) { | ||
| + | width = initWidth; | ||
| + | height = initHeight; | ||
| + | origin = initOrigin; | ||
| } | } | ||
| | | ||
| - | public Rectangle(int | + | public Rectangle(int |
| - | width = setWidth; | + | width = initWidth; |
| - | height = setHeight; | + | height = initHeight; |
| + | origin = new Point(xPos, | ||
| } | } | ||
| .... | .... | ||
| Line 50: | Line 75: | ||
| </ | </ | ||
| - | __Το παραπάνω, είναι αποδεκτό από τον compiler της Java__. Ο τελεστής **this** χρησιμοποιείται για | + | Ιδανικά θα θέλαμε να καλέσουμε έναν κατασκευαστή |
| - | <WRAP important 80% center round> | ||
| - | Η κλήση ενός κατασκευαστή μέσα από άλλο κατασκευαστή θα πρέπει να γίνει στην αρχή του σώματος του κατασκευατή. Το παρακάτω παράδειγμα βγάζει μήνυμα λάθους από τον compiler. | ||
| <code java> | <code java> | ||
| public class Rectangle { | public class Rectangle { | ||
| Line 61: | Line 84: | ||
| private Point origin; | private Point origin; | ||
| | | ||
| - | | + | public Rectangle(int |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| } | } | ||
| | | ||
| - | public Rectangle(int | + | public Rectangle(int |
| - | | + | |
| - | height = setHeight; | + | |
| } | } | ||
| .... | .... | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | <WRAP tip 80% center round> | ||
| + | Ο τελεστής **this** χρησιμοποιείται για να κληθεί από ένα κατασκευαστή ένας άλλος κατασκευαστής της ίδιας κλάσης. | ||
| </ | </ | ||
| - | ===== Πρόσβαση σε πεδία και μεθόδους των | + | <WRAP important 80% center round> |
| - | + | Η κλήση ενός κατασκευαστή | |
| - | Ας ξαναγράψουμε το παρακάτω τμήμα | + | |
| <code java> | <code java> | ||
| Line 88: | Line 111: | ||
| private Point origin; | private Point origin; | ||
| | | ||
| - | // the Rectangle class has one constructor | ||
| public Rectangle(int width, int height, Point origin) { | public Rectangle(int width, int height, Point origin) { | ||
| - | | + | |
| - | height | + | |
| - | | + | * κατασκευαστή, |
| + | * παράγει μήνυμα λάθους από τον compiler. | ||
| + | */ | ||
| + | this(width, | ||
| + | | ||
| } | } | ||
| - | ... | + | |
| + | public Rectangle(int width, int height) { | ||
| + | this.width = width; | ||
| + | this.height = height; | ||
| + | } | ||
| + | .... | ||
| } | } | ||
| </ | </ | ||
| + | </ | ||
| - | Στον παραπάνω κώδικα οι τοπικές παράμετροι του κατασκευαστή συμπίπτουν με τα ονόματα των μεταβλητών της κλάσης. Σε αυτή την περίπτωση η τοπική μεταβλητή " | ||
| - | <code java> | + | <WRAP info 80% center round> |
| + | Συμπερασματικά, | ||
| + | * για να διακρίνεται τα πεδία του τρέχοντος αντικειμένου σε σχέση με τοπικές μεταβλητές που έχουν το ίδιο όνομα με τα πεδία αυτά. | ||
| + | * να καλέσετε τον κατασκευαστή της κλάσης για το τρέχον αντικείμενο. | ||
| + | |||
| + | </ | ||
| + | |||
| + | Παρακάτω, | ||
| + | |||
| + | < | ||
| public class Rectangle { | public class Rectangle { | ||
| | | ||
| + | // the Rectangle class has 3 fields | ||
| private int width; | private int width; | ||
| private int height; | private int height; | ||
| private Point origin; | private Point origin; | ||
| | | ||
| - | // the Rectangle class has one constructor | + | // the Rectangle class has two constructors |
| public Rectangle(int width, int height, Point origin) { | public Rectangle(int width, int height, Point origin) { | ||
| this.width = width; | this.width = width; | ||
| this.height = height; | this.height = height; | ||
| - | this.origin = o; | + | this.origin = origin; |
| + | } | ||
| + | |||
| + | public Rectangle(int width, int height, int xPos, int yPos) { | ||
| + | this(width, height, new Point(xPos, yPos)); | ||
| + | } | ||
| + | |||
| + | public void setWidth(int width ) { | ||
| + | this.width = width; | ||
| + | } | ||
| + | |||
| + | public int getWidth() { | ||
| + | return width; | ||
| + | } | ||
| + | |||
| + | public void setHeight(int height ) { | ||
| + | this.height = height; | ||
| + | } | ||
| + | |||
| + | public int getHeight() { | ||
| + | return height; | ||
| + | } | ||
| + | |||
| + | public void setOrigin(Point | ||
| + | origin = o; | ||
| + | } | ||
| + | |||
| + | public Point getOrigin() { | ||
| + | return origin; | ||
| + | } | ||
| + | |||
| + | public int getArea() { | ||
| + | | ||
| + | } | ||
| + | |||
| + | // Move rectangle origin by dx,dy | ||
| + | public void moveOrigin(int dx, int dy) { | ||
| + | origin.setX( origin.getX() + dx ); | ||
| + | origin.setY( origin.getY() + dy ); | ||
| } | } | ||
| - | ... | ||
| } | } | ||
| </ | </ | ||
| - | __O τελεστής **this** υποδεικνύει το τρέχον αντικείμενο__. Με τον παραπάνω τρόπο μπορούμε να διακρίνουμε μία μεταβλητής του τρέχοντος αντικειμένου (this.variable) από μία τοπική μεταβλητή (variable) με το ίδιο όνομα. | + | |Προηγούμενο: |
| - | + | ||
| - | <WRAP tip 80% center round> | + | |
| - | O τελεστής **this** δείχνει στο τρέχον αντικείμενο της εκάστοτε κλάσης, | + | |
| - | </ | + | |
| - | + | ||
| - | |Προηγούμενο: | + | |