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 12:16]
gthanos [Ο τελεστής this]
java:this_operator [2020/02/21 12:20]
gthanos [Κλήση ενός κατασκευαστή μέσω άλλου κατασκευαστή με χρήση του τελεστή this]
Line 2: Line 2:
  
 Μέσω του τελεστή **this** μπορούμε:  Μέσω του τελεστή **this** μπορούμε: 
-  * να έχουμε πρόσβαση μέσα σε μία μέθοδο στα πεδία του τρέχοντος αντικειμένου όταν υπάρχουν και τοπικές μεταβλητές που έχουν το ίδιο όνομα με το αντικείμενο.+  * να έχουμε πρόσβαση στα πεδία του τρέχοντος αντικειμένου όταν υπάρχουν και τοπικές μεταβλητές που έχουν το ίδιο όνομα με το αντικείμενο.
   * κατά τη δημιουργία του αντικειμένου να καλέσουμε ένα κατασκευαστή μέσω ενός άλλου κατασκευαστή.   * κατά τη δημιουργία του αντικειμένου να καλέσουμε ένα κατασκευαστή μέσω ενός άλλου κατασκευαστή.
  
-===== Πρόσβαση σε πεδία και μεθόδους των αντικειμένων της κλάσης μέσω του τελεστή this =====+===== Πρόσβαση σε πεδία και μεθόδους των αντικειμένων της κλάσης με χρήση του τελεστή this =====
  
 Ας ξαναγράψουμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα, ως εξής Ας ξαναγράψουμε το παρακάτω τμήμα κώδικα το οποίο συναντήσαμε προηγούμενα, ως εξής
Line 54: Line 54:
  
 <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 66: 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 78: 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 90: 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 96: Line 96:
 } }
 </code> </code>
- 
-<WRAP tip 80% center round> 
-Ο τελεστής **this** χρησιμοποιείται για να κληθεί από ένα κατασκευαστή ένας άλλος κατασκευαστής της ίδιας κλάσης. 
-</WRAP> 
  
 <WRAP important 80% center round> <WRAP important 80% center round>
Line 105: 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 121: 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 135: Line 131:
   * για να διακρίνεται τα πεδία του τρέχοντος αντικειμένου σε σχέση με τοπικές μεταβλητές που έχουν το ίδιο όνομα με τα πεδία αυτά.    * για να διακρίνεται τα πεδία του τρέχοντος αντικειμένου σε σχέση με τοπικές μεταβλητές που έχουν το ίδιο όνομα με τα πεδία αυτά. 
   * να καλέσετε τον κατασκευαστή της κλάσης για το τρέχον αντικείμενο.   * να καλέσετε τον κατασκευαστή της κλάσης για το τρέχον αντικείμενο.
- 
 </WRAP> </WRAP>
  
Line 141: 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 155: 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 );
java/this_operator.txt · Last modified: 2022/02/23 16:10 by gthanos