This shows you the differences between two versions of the page.
|
java:member_initialization [2016/02/19 14:09] doufexi [Static Initialization Blocks] |
java:member_initialization [2022/02/24 12:39] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Αρχικοποίηση των μελών της κλάσης ====== | ||
| - | |||
| - | Στην γλώσσα προγραμματισμού Java μπορούμε να αρχικοποιήσουμε την τιμή ενός πεδίου στο σώμα της κλάσης, | ||
| - | |||
| - | <code java> | ||
| - | public class BedAndBreakfast { | ||
| - | |||
| - | // initialize to 10 | ||
| - | public static int capacity = 10; | ||
| - | |||
| - | // initialize to false | ||
| - | private boolean full = false; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Η παραπάνω μέθοδος αφορά **στατικά** (static) και **μη στατικά** πεδία, προϋποθέτει όμως ότι η ανάθεση της τιμής μπορεί αν γίνει μόνο με μία εντολή (όπως παραπάνω). Πιο σύνθετες μέθοδοι ανάθεσης (π.χ. πρέπει να γίνει αρχικοποίηση ενός πίνακα) μπορούν να γίνουν μέσα στους κατασκευαστές τις κλάσης, | ||
| - | |||
| - | Για την αρχικοποίηση πεδίων εκτός από τους κατασκευαστές έχουμε τις εξής επιλογές: | ||
| - | * για στατικά πεδία | ||
| - | * **static initialization blocks**. | ||
| - | * **static methods** (στατικές μέθοδοι). | ||
| - | * για μη-στατικά πεδία | ||
| - | * **initialization blocks** (non-static). | ||
| - | * **final methods** | ||
| - | |||
| - | ===== Αρχικοποιώντας στατικά πεδία ===== | ||
| - | |||
| - | ==== Static Initialization Blocks ==== | ||
| - | |||
| - | To //static initialization block// είναι ένα τμήμα κώδικα τοποθετημένο μέσα σε άγκιστρα { } που προσδιορίζεται από τη λέξη //static//, όπως παρακάτω: | ||
| - | |||
| - | <code java> | ||
| - | static { | ||
| - | ... | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Κάθε κλάση μπορεί να έχει περισσότερα από ένα //static initialization blocks//, τα οποία μπορεί να βρίσκονται οπουδήποτε μέσα στο σώμα της. Τα block αυτά καλούνται κατά την αρχικοποίηση της κλάσης με τη σειρά που εμφανίζονται στο σώμα της. | ||
| - | |||
| - | ==== Στατικές Μέθοδοι ==== | ||
| - | |||
| - | Εναλλακτικά των //static initialization blocks// μπορούν να χρησιμοποιηθούν στατικές μέθοδοι, | ||
| - | <code java> | ||
| - | class MyClass { | ||
| - | public static varType myVar = initializeClassVariable(); | ||
| - | | ||
| - | private static varType initializeClassVariable() { | ||
| - | |||
| - | // initialization code goes here | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Το πλεονέκτημα των στατικών μεθόδων είναι ότι μπορούν να επαναχρησιμοποιηθούν σε περίπτωση που χρειαστεί να επανα-αρχικοποιηθεί η στατική μεταβλητή της κλάσης. | ||
| - | |||
| - | ===== Αρχικοποιώντας μη-στατικά πεδία ===== | ||
| - | |||
| - | ==== Initialization Blocks ==== | ||
| - | |||
| - | Τα // | ||
| - | |||
| - | ==== Final Methods ==== | ||
| - | |||
| - | Παρακάτω δίνεται ένα παράδειγμα χρήσης //final// μεθόδου για την αρχικοποίηση ενός μη στατικού πεδίου. Μία //final// μέθοδος δεν μπορεί να γίνει // | ||
| - | |||
| - | <code java> | ||
| - | class Whatever { | ||
| - | private varType myVar = initializeInstanceVariable(); | ||
| - | | ||
| - | protected final varType initializeInstanceVariable() { | ||
| - | |||
| - | // initialization code goes here | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Προφανώς η μέθοδος μπορεί να επαναχρησιμοποιηθεί και σε άλλα σημεία του κώδικα της κλάσης. Η χρήση //final// μεθόδου κρίνεται απαραίτητη για την αρχικοποίηση μη στατικών πεδίων, | ||
| - | |||
| - | <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(); | ||
| - | | ||
| - | // the Rectangle class has one constructor | ||
| - | public Rectangle(int setWidth, int setHeight, Point o) { | ||
| - | this(setWidth, | ||
| - | origin = o; | ||
| - | } | ||
| - | | ||
| - | public Rectangle(int setWidth, int setHeight) { | ||
| - | width = setWidth; | ||
| - | height = setHeight; | ||
| - | id = ++numberOfRectangles; | ||
| - | System.out.println(" | ||
| - | } | ||
| - | | ||
| - | | ||
| - | // static method for initializing static variable. | ||
| - | private static int initNumberOfRectangles() { | ||
| - | System.out.println(" | ||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | //static initialization block | ||
| - | static { | ||
| - | System.out.println(" | ||
| - | numberOfRectangles = 0; | ||
| - | } | ||
| - | |||
| - | // | ||
| - | { | ||
| - | id = 100; | ||
| - | System.out.println(" | ||
| - | } | ||
| - | | ||
| - | //final method | ||
| - | private final int initializeId() { | ||
| - | int _id = 200; | ||
| - | System.out.println(" | ||
| - | return _id; | ||
| - | } | ||
| - | | ||
| - | public int getID() { | ||
| - | return id; | ||
| - | } | ||
| - | |||
| - | public static int getNumberOfRectangles() { | ||
| - | return numberOfRectangles; | ||
| - | } | ||
| - | | ||
| - | // the Rectangel class has 3 methods | ||
| - | 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 o) { | ||
| - | origin = o; | ||
| - | } | ||
| - | | ||
| - | public Point getOrigin() { | ||
| - | return origin; | ||
| - | } | ||
| - | | ||
| - | public int getArea() { | ||
| - | | ||
| - | } | ||
| - | |||
| - | // 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 " | ||
| - | } | ||
| - | | ||
| - | public static void main(String []args) { | ||
| - | System.out.println(" | ||
| - | Point p = new Point(10, | ||
| - | Rectangle rectOne = new Rectangle(30, | ||
| - | System.out.println(rectOne.toString(" | ||
| - | System.out.println(" | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Το αποτέλεσμα εκτέλεσης το παραπάνω κώδικα είναι το παρακάτω: | ||
| - | < | ||
| - | $ 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, | ||
| - | [rectOne.1] Width: 30, Height: 40, Origin: [Point xPos:10, yPos:20] | ||
| - | ------------------------------- | ||
| - | Number of Rectangles: 1 | ||
| - | </ | ||
| - | |||
| - | |Προηγούμενο: | ||
| - | |||
| - | |||
| - | |||
| - | |||