User Tools

Site Tools


java:this_operator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
java:this_operator [2020/02/21 11:58]
gthanos [Πρόσβαση στους κατασκευαστές της κλάσης μέσω του τελεστή this]
java:this_operator [2020/02/21 12:20]
gthanos [Κλήση ενός κατασκευαστή μέσω άλλου κατασκευαστή με χρήση του τελεστή this]
Line 1: Line 1:
 ======  Ο τελεστής this ====== ======  Ο τελεστής this ======
  
-===== Πρόσβαση σε πεδία και μεθόδους των αντικειμένων της κλάσης μέσω του τελεστή this =====+Μέσω του τελεστή **this** μπορούμε:  
 +  * να έχουμε πρόσβαση στα πεδία του τρέχοντος αντικειμένου όταν υπάρχουν και τοπικές μεταβλητές που έχουν το ίδιο όνομα με το αντικείμενο. 
 +  * κατά τη δημιουργία του αντικειμένου να καλέσουμε ένα κατασκευαστή μέσω ενός άλλου κατασκευαστή. 
 + 
 +===== Πρόσβαση σε πεδία και μεθόδους των αντικειμένων της κλάσης με χρήση του τελεστή this =====
  
 Ας ξαναγράψουμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα, ως εξής Ας ξαναγράψουμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα, ως εξής
Line 45: Line 49:
 </WRAP> </WRAP>
  
-===== Πρόσβαση στους κατασκευαστές της κλάσης μέσω του τελεστή this =====+===== Κλήση ενός κατασκευαστή μέσω άλλου κατασκευαστή με χρήση του τελεστή this =====
  
 Ας ξαναδούμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα θέλοντας να επιχειρήσουμε να επαναχρησιμοποιήσουμε τμήματα κώδικα ενός κατασκευαστή από έναν άλλο κατασκευαστή.  Ας ξαναδούμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα θέλοντας να επιχειρήσουμε να επαναχρησιμοποιήσουμε τμήματα κώδικα ενός κατασκευαστή από έναν άλλο κατασκευαστή. 
  
 <code java> <code java>
-public class Rectangle {+class Rectangle {
          
-  private int width; +  int width; 
-  private int height; +  int height; 
-  private Point origin;+  Point origin;
          
-  public Rectangle(int initWidth, int initHeight, Point initOrigin) {+  Rectangle(int initWidth, int initHeight, Point initOrigin) {
     width = initWidth;     width = initWidth;
     height = initHeight;     height = initHeight;
Line 62: Line 66:
   }   }
      
-  public Rectangle(int initWidth, int initHeight, int xPos, int yPos) {+  Rectangle(int initWidth, int initHeight, int xPos, int yPos) {
     width = initWidth;     width = initWidth;
     height = initHeight;     height = initHeight;
Line 74: Line 78:
  
 <code java> <code java>
-public class Rectangle {+class Rectangle {
          
-  private int width; +  int width; 
-  private int height; +  int height; 
-  private Point origin;+  Point origin;
          
-  public Rectangle(int initWidth, int initHeight, Point initOrigin) {+  Rectangle(int initWidth, int initHeight, Point initOrigin) {
     width = initWidth;     width = initWidth;
     height = initHeight;     height = initHeight;
Line 86: Line 90:
   }   }
      
-  public Rectangle(int initWidth, int initHeight, int xPos, int yPos) {+  Rectangle(int initWidth, int initHeight, int xPos, int yPos) {
     this(initWidth, initHeight, new Point(xPos,yPos));     this(initWidth, initHeight, new Point(xPos,yPos));
   }   }
Line 92: Line 96:
 } }
 </code> </code>
- 
-<WRAP tip 80% center round> 
-Ο τελεστής **this** χρησιμοποιείται για να κληθεί από ένα κατασκευαστή ένας άλλος κατασκευαστής της ίδιας κλάσης. 
-</WRAP> 
  
 <WRAP important 80% center round> <WRAP important 80% center round>
Line 101: Line 101:
  
 <code java> <code java>
-public class Rectangle {+class Rectangle {
          
-  private int width; +  int width; 
-  private int height; +  int height; 
-  private Point origin;+  Point origin;
          
-  public Rectangle(int width, int height, Point origin) {+  Rectangle(int width, int height, Point origin) {
     this.origin = origin;     this.origin = origin;
     /* η κλήση ενός κατασκευαστή μέσα από άλλο     /* η κλήση ενός κατασκευαστή μέσα από άλλο
Line 117: Line 117:
   }   }
      
-  public Rectangle(int width, int height) {+  Rectangle(int width, int height) {
     this.width = width;     this.width = width;
     this.height = height;     this.height = height;
Line 131: Line 131:
   * για να διακρίνεται τα πεδία του τρέχοντος αντικειμένου σε σχέση με τοπικές μεταβλητές που έχουν το ίδιο όνομα με τα πεδία αυτά.    * για να διακρίνεται τα πεδία του τρέχοντος αντικειμένου σε σχέση με τοπικές μεταβλητές που έχουν το ίδιο όνομα με τα πεδία αυτά. 
   * να καλέσετε τον κατασκευαστή της κλάσης για το τρέχον αντικείμενο.   * να καλέσετε τον κατασκευαστή της κλάσης για το τρέχον αντικείμενο.
- 
 </WRAP> </WRAP>
  
Line 137: Line 136:
  
 <code java Rectangle.java> <code java Rectangle.java>
-public class Rectangle {+class Rectangle {
          
   // the Rectangle class has 3 fields   // the Rectangle class has 3 fields
-  private int width; +  int width; 
-  private int height; +  int height; 
-  private Point origin;+  Point origin;
          
   // the Rectangle class has two constructors   // the Rectangle class has two constructors
-  public Rectangle(int width, int height, Point origin) {+  Rectangle(int width, int height, Point origin) {
     this.width = width;     this.width = width;
     this.height = height;     this.height = height;
Line 151: Line 150:
   }   }
      
-  public Rectangle(int width, int height, int xPos, int yPos) {+  Rectangle(int width, int height, int xPos, int yPos) {
     this(width, height, new Point(xPos, yPos));     this(width, height, new Point(xPos, yPos));
   }   }
          
-  public void setWidth(int width ) {+  void setWidth(int width ) {
     this.width = width;     this.width = width;
   }   }
      
-  public int getWidth() {+  int getWidth() {
     return width;     return width;
   }   }
          
-  public void setHeight(int height ) {+  void setHeight(int height ) {
     this.height = height;     this.height = height;
   }   }
      
-  public int getHeight() {+  int getHeight() {
     return height;     return height;
   }   }
          
-  public void setOrigin(Point o) {+  void setOrigin(Point o) {
     origin = o;     origin = o;
   }   }
      
-  public Point getOrigin() {+  Point getOrigin() {
     return origin;     return origin;
   }   }
      
-  public int getArea() {+  int getArea() {
        return width * height;        return width * height;
   }   }
        
   // Move rectangle origin by dx,dy   // Move rectangle origin by dx,dy
-  public void moveOrigin(int dx, int 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 );
Line 191: Line 190:
 </code> </code>
  
-|Προηγούμενο: [[:java:java:arrays | Πίνακες]] | [[:toc | Περιεχόμενα ]] | Επόμενο: [[:java:static_keyword | Στατικές μεταβλητές και μέθοδοι ]]|+|Προηγούμενο: [[:java:arrays | Πίνακες]] | [[:toc | Περιεχόμενα ]] | Επόμενο: [[:java:static_keyword | Στατικές μεταβλητές και μέθοδοι ]]|
  
java/this_operator.txt · Last modified: 2022/02/23 16:10 by gthanos