java:member_initialization

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 Both sides next revision
java:member_initialization [2016/02/19 14:09]
doufexi [Static Initialization Blocks]
java:member_initialization [2016/02/19 14:22]
doufexi fixed contructor, added toc link
Line 91: Line 91:
   private static int numberOfRectangles = initNumberOfRectangles();​   private static int numberOfRectangles = initNumberOfRectangles();​
     ​     ​
-  ​// the Rectangle class has one constructor +  public Rectangle(int ​initWidth, int initHeight, Point initOrigin) { 
-  ​public Rectangle(int ​setWidth, int setHeight, Point o) { +    this(initHeightinitHeight); 
-    this(setWidthsetHeight); +    origin = initOrigin;
-    origin = o;+
   }   }
   ​   ​
-  public Rectangle(int ​setWidth, int setHeight) { +  public Rectangle(int ​initWidth, int initHeight) { 
-    width = setWidth+    width = initWidth
-    height = setHeight;+    height = initHeight;​ 
 +    origin = new Point(0,0);
     id = ++numberOfRectangles;​     id = ++numberOfRectangles;​
-    System.out.println("​Initialized object id in constructor,​ id:"​+id);​+    System.out.println("​Initialized object id in constructor,​ id:" + id);
   }   }
   ​   ​
Line 120: Line 120:
   {   {
     id = 100;     id = 100;
-    System.out.println("​initialize objectId in block, id: "+id);+    System.out.println("​initialize objectId in block, id: " + id);
   }   }
   ​   ​
Line 126: Line 126:
   private final int initializeId() {   private final int initializeId() {
     int _id = 200;     int _id = 200;
-    System.out.println("​initialize objectId in final method, id: "​+_id ​ );+    System.out.println("​initialize objectId in final method, id: " + _id  );
     return _id;     return _id;
   }   }
Line 138: Line 138:
   }   }
     ​     ​
-  // the Rectangel class has 3 methods 
   public void setWidth(int newWidth ) {   public void setWidth(int newWidth ) {
     width = newWidth;     width = newWidth;
Line 155: Line 154:
   }   }
     ​     ​
-  public void setOrigin(Point ​o) { +  public void setOrigin(Point ​newOrigin) { 
-    origin = o;+    origin = newOrigin;
   }   }
   ​   ​
Line 174: Line 173:
   ​   ​
   public String toString(String objName) {   public String toString(String objName) {
-      return "​["​+objName+"​."​+id+"​] Width: "​+width+",​ Height: "​+height+",​ Origin: "​+origin;​+      return "​["​+objName+"​."​ + id + "] Width: " + width + ", Height: " + height + ", Origin: " + origin;
   }   }
   ​   ​
Line 183: Line 182:
     System.out.println(rectOne.toString("​rectOne"​));​     System.out.println(rectOne.toString("​rectOne"​));​
     System.out.println("​-------------------------------"​);​     System.out.println("​-------------------------------"​);​
-    System.out.println("​Number of Rectangles: "​+getNumberOfRectangles() );+    System.out.println("​Number of Rectangles: " + getNumberOfRectangles() );
   }   }
 } }
 </​code>​ </​code>​
  
-Το αποτέλεσμα εκτέλεσης το παραπάνω κώδικα είναι το παρακάτω:​+Το αποτέλεσμα εκτέλεσης του παραπάνω κώδικα είναι το παρακάτω:​
 <​code>​ <​code>​
 $ java Rectangle $ java Rectangle
Line 202: Line 201:
 </​code>​ </​code>​
  
-|Προηγούμενο:​ [[:​java:​static_keyword | Στατικές μεταβλητές και μέθοδοι ]] | Επόμενο:​ [[ :​java:​access_modifiers | Περιοριστές πρόσβασης ]]|+|Προηγούμενο:​ [[:​java:​static_keyword | Στατικές μεταβλητές και μέθοδοι ​]] | [[ :toc | Περιεχόμενα ​]] | Επόμενο:​ [[ :​java:​access_modifiers | Περιοριστές πρόσβασης ]]|
  
  
java/member_initialization.txt · Last modified: 2017/02/16 11:29 by gthanos