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
Previous revision
Last revision Both sides next revision
java:member_initialization [2015/02/17 12:52]
gthanos [Αρχικοποίηση των μελών της κλάσης]
java:member_initialization [2016/02/26 11:15]
127.0.0.1 external edit
Line 28: Line 28:
 ==== Static Initialization Blocks ==== ==== Static Initialization Blocks ====
  
-To //static initialization block// είναι ένα τμήμα κώδικα τοποθετημένο μέσα σε παρενθέσεις ​{ } που προσδιορίζεται από το keyword ​static, όπως παρακάτω:​+To //static initialization block// είναι ένα τμήμα κώδικα τοποθετημένο μέσα σε άγκιστρα { } που προσδιορίζεται από τη λέξη //static//, όπως παρακάτω:​
  
 <code java> <code java>
Line 77: Line 77:
 Προφανώς η μέθοδος μπορεί να επαναχρησιμοποιηθεί και σε άλλα σημεία του κώδικα της κλάσης. Η χρήση //final// μεθόδου κρίνεται απαραίτητη για την αρχικοποίηση μη στατικών πεδίων,​ διότι σε περίπτωση ύπαρξης κληρονόμων της κλάσης μπορεί να υπάρξουν προβλήματα. Προφανώς η μέθοδος μπορεί να επαναχρησιμοποιηθεί και σε άλλα σημεία του κώδικα της κλάσης. Η χρήση //final// μεθόδου κρίνεται απαραίτητη για την αρχικοποίηση μη στατικών πεδίων,​ διότι σε περίπτωση ύπαρξης κληρονόμων της κλάσης μπορεί να υπάρξουν προβλήματα.
  
-|Προηγούμενο:​ [[:​java:​static_keyword | Στατικές μεταβλητές και μέθοδοι ]] | Επόμενο:​ [[ :java:packages:​intro ​Πακέτα στη γλώσσα προγραμματισμού Java ]]|+<code java Rectangle.java>​ 
 +public class Rectangle { 
 +     
 +  // the Rectangle class has 3 fields 
 +  private int width; 
 +  private int height; 
 +  private Point origin; 
 +   
 +  private int id = initializeId();​ 
 +  
 +  // add a class variable for the 
 +  // number of Rectangle objects instantiated 
 +  private static int numberOfRectangles = initNumberOfRectangles();​ 
 +     
 +  public Rectangle(int initWidth, int initHeight, Point initOrigin) { 
 +    this(initHeight,​ initHeight);​ 
 +    origin = initOrigin;​ 
 +  } 
 +   
 +  public Rectangle(int initWidth, int initHeight) { 
 +    width = initWidth;​ 
 +    height = initHeight;​ 
 +    origin = new Point(0,​0);​ 
 +    id = ++numberOfRectangles;​ 
 +    System.out.println("​Initialized object id in constructor,​ id:" + id); 
 +  } 
 +   
 +   
 +  // static method for initializing static variable. 
 +  private static int initNumberOfRectangles() { 
 +    System.out.println("​initialize numberOfRectangles in static method"​);​ 
 +    return 0; 
 +  } 
 + 
 +  //static initialization block  
 +  static { 
 +    System.out.println("​initialize numberOfRectangles in static block"​);​ 
 +    numberOfRectangles = 0; 
 +  } 
 + 
 +  //​non-static initialization block  
 +  { 
 +    id = 100; 
 +    System.out.println("​initialize objectId in block, id: " + id); 
 +  } 
 +   
 +  //final method 
 +  private final int initializeId() { 
 +    int _id = 200; 
 +    System.out.println("​initialize objectId in final method, id: " + _id  ); 
 +    return _id; 
 +  } 
 +   
 +  public int getID() { 
 +    return id; 
 +  } 
 +  
 +  public static int getNumberOfRectangles() { 
 +    return numberOfRectangles;​ 
 +  } 
 +     
 +  public void setWidth(int newWidth ) { 
 +    width = newWidth; 
 +  } 
 +   
 +  public int getWidth() { 
 +    return width; 
 +  } 
 +     
 +  public void setHeight(int newHeight ) { 
 +    height = newHeight;​ 
 +  } 
 +   
 +  public int getHeight() { 
 +    return height; 
 +  } 
 +     
 +  public void setOrigin(Point newOrigin) { 
 +    origin = newOrigin;​ 
 +  } 
 +   
 +  public Point getOrigin() { 
 +    return origin; 
 +  } 
 +   
 +  public int getArea() { 
 +       ​return width * height; 
 +  } 
 +    
 +  // Move rectangle origin by x,y 
 +  public void move(int dx, int dy) { 
 +    origin.setX( origin.getX() + dx ); 
 +    origin.setY( origin.getY() + dy ); 
 +  } 
 +   
 +  public String toString(String objName) { 
 +      return "​["​+objName+"​."​ + id + "] Width: " + width + ", Height: " + height + ", Origin: " + origin; 
 +  } 
 +   
 +  public static void main(String []args) { 
 +    System.out.println("​-------------------------------"​);​ 
 +    Point p = new Point(10,​20);​ 
 +    Rectangle rectOne = new Rectangle(30,​40,​ p); 
 +    System.out.println(rectOne.toString("​rectOne"​));​ 
 +    System.out.println("​-------------------------------"​);​ 
 +    System.out.println("​Number of Rectangles: " + getNumberOfRectangles() ); 
 +  } 
 +
 +</​code>​ 
 + 
 +Το αποτέλεσμα εκτέλεσης του παραπάνω κώδικα είναι το παρακάτω:​ 
 +<​code>​ 
 +$ java Rectangle 
 +initialize numberOfRectangles in static method 
 +initialize numberOfRectangles in static block 
 +------------------------------- 
 +initialize objectId in final method, id: 200 
 +initialize objectId in block, id: 100 
 +Initialized object id in constructor,​ id:1 
 +[rectOne.1] Width: 30, Height: 40, Origin: [Point xPos:10, yPos:20] 
 +------------------------------- 
 +Number of Rectangles: 1 
 +</​code>​ 
 + 
 +|Προηγούμενο:​ [[:​java:​static_keyword | Στατικές μεταβλητές και μέθοδοι ​]] | [[ :toc | Περιεχόμενα ​]] | Επόμενο:​ [[ :java:toString ​Η μέθοδος toString ​]]|
  
  
java/member_initialization.txt · Last modified: 2017/02/16 11:29 by gthanos