This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
|
java:this_operator [2016/01/24 17:16] gthanos [Πρόσβαση σε πεδία και μεθόδους των αντικειμένων της κλάσης μέσω του τελεστή this] |
java:this_operator [2020/02/21 12:20] 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.origin = origin; | ||
| } | } | ||
| - | | + | ... |
| - | public Rectangle(int setWidth, int setHeight) { | + | |
| - | width = setWidth; | + | |
| - | height = setHeight; | + | |
| - | } | + | |
| - | .... | + | |
| } | } | ||
| </ | </ | ||
| - | __Το παραπάνω, είναι αποδεκτό από τον | + | <WRAP tip 80% center round> |
| + | __O τελεστής this υποδεικνύει το τρέχον αντικείμενο__. Με τον παραπάνω | ||
| + | </ | ||
| + | |||
| + | ===== Κλήση ενός | ||
| + | |||
| + | Ας ξαναδούμε | ||
| - | <WRAP important 80% center round> | ||
| - | Η κλήση ενός κατασκευαστή μέσα από άλλο κατασκευαστή θα πρέπει να γίνει στην αρχή του σώματος του κατασκευατή. Το παρακάτω παράδειγμα βγάζει μήνυμα λάθους από τον compiler. | ||
| <code java> | <code java> | ||
| - | public | + | class Rectangle { |
| | | ||
| - | | + | int width; |
| - | | + | int height; |
| - | | + | Point origin; |
| - | + | ||
| - | // the Rectangle class has one constructor | + | |
| - | public Rectangle(int setWidth, int setHeight, Point o) { | + | |
| - | origin = o; | + | |
| - | this(setWidth, | + | |
| | | ||
| + | Rectangle(int initWidth, int initHeight, Point initOrigin) { | ||
| + | width = initWidth; | ||
| + | height = initHeight; | ||
| + | origin = initOrigin; | ||
| } | } | ||
| | | ||
| - | | + | Rectangle(int |
| - | width = setWidth; | + | width = initWidth; |
| - | height = setHeight; | + | height = initHeight; |
| + | origin = new Point(xPos, | ||
| } | } | ||
| .... | .... | ||
| } | } | ||
| </ | </ | ||
| - | </ | ||
| - | ===== Πρόσβαση σε πεδία και μεθόδους των αντικειμένων της κλάσης μέσω του τελεστή this ===== | + | Ιδανικά θα θέλαμε να καλέσουμε έναν κατασκευαστή μέσα |
| - | + | ||
| - | Ας ξαναγράψουμε | + | |
| <code java> | <code java> | ||
| - | public | + | class Rectangle { |
| | | ||
| - | | + | int width; |
| - | | + | int height; |
| - | | + | Point origin; |
| | | ||
| - | | + | Rectangle(int |
| - | public | + | width = initWidth; |
| - | width = width; | + | height = initHeight; |
| - | height = height; | + | origin = initOrigin; |
| - | origin = o; | + | |
| } | } | ||
| - | ... | + | |
| + | Rectangle(int initWidth, int initHeight, int xPos, int yPos) { | ||
| + | this(initWidth, | ||
| + | } | ||
| + | .... | ||
| } | } | ||
| </ | </ | ||
| - | Στον παραπάνω κώδικα οι τοπικές παράμετροι του | + | <WRAP important 80% center round> |
| + | Η κλήση ενός κατασκευαστή μέσα από άλλο κατασκευαστή | ||
| <code java> | <code java> | ||
| - | public | + | class Rectangle { |
| | | ||
| - | | + | int width; |
| - | | + | int height; |
| - | | + | Point origin; |
| | | ||
| - | // the Rectangle class has one constructor | + | |
| - | | + | this.origin = origin; |
| + | | ||
| + | * κατασκευαστή, | ||
| + | * παράγει μήνυμα λάθους από τον compiler. | ||
| + | */ | ||
| + | this(width, height); | ||
| + | |||
| + | } | ||
| + | | ||
| + | Rectangle(int width, int height) { | ||
| this.width = width; | this.width = width; | ||
| this.height = height; | this.height = height; | ||
| - | this.origin = o; | ||
| } | } | ||
| - | ... | + | |
| } | } | ||
| </ | </ | ||
| + | </ | ||
| - | __O τελεστής **this** υποδεικνύει το τρέχον αντικείμενο__. Με τον παραπάνω τρόπο μπορούμε να διακρίνουμε μία μεταβλητής του τρέχοντος αντικειμένου (this.variable) από μία τοπική μεταβλητή (variable) με το ίδιο όνομα. | ||
| - | < | + | < |
| - | O τελεστής **this** δείχνει στο τρέχον αντικείμενο της εκάστοτε κλάσης, | + | Συμπερασματικά, |
| + | * για να διακρίνεται τα πεδία του | ||
| + | * να καλέσετε | ||
| </ | </ | ||
| - | |Προηγούμενο: | + | Παρακάτω, |
| + | |||
| + | <code java Rectangle.java> | ||
| + | class Rectangle { | ||
| + | |||
| + | // the Rectangle class has 3 fields | ||
| + | int width; | ||
| + | int height; | ||
| + | Point origin; | ||
| + | |||
| + | // the Rectangle class has two constructors | ||
| + | Rectangle(int width, int height, Point origin) { | ||
| + | this.width = width; | ||
| + | this.height = height; | ||
| + | this.origin = origin; | ||
| + | } | ||
| + | |||
| + | Rectangle(int width, int height, int xPos, int yPos) { | ||
| + | this(width, height, new Point(xPos, yPos)); | ||
| + | } | ||
| + | |||
| + | void setWidth(int width ) { | ||
| + | this.width = width; | ||
| + | } | ||
| + | |||
| + | int getWidth() { | ||
| + | return width; | ||
| + | } | ||
| + | |||
| + | void setHeight(int height ) { | ||
| + | this.height = height; | ||
| + | } | ||
| + | |||
| + | int getHeight() { | ||
| + | return height; | ||
| + | } | ||
| + | |||
| + | void setOrigin(Point o) { | ||
| + | origin = o; | ||
| + | } | ||
| + | |||
| + | Point getOrigin() { | ||
| + | return origin; | ||
| + | } | ||
| + | |||
| + | int getArea() { | ||
| + | | ||
| + | } | ||
| + | |||
| + | // Move rectangle origin by dx,dy | ||
| + | void moveOrigin(int dx, int dy) { | ||
| + | origin.setX( origin.getX() + dx ); | ||
| + | origin.setY( origin.getY() + dy ); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |Προηγούμενο: | ||