This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
java:member_initialization [2016/02/10 07:54] gthanos [Static Initialization Blocks] |
java:member_initialization [2016/02/11 09:35] gthanos [Final Methods] |
||
---|---|---|---|
Line 77: | Line 77: | ||
Προφανώς η μέθοδος μπορεί να επαναχρησιμοποιηθεί και σε άλλα σημεία του κώδικα της κλάσης. Η χρήση //final// μεθόδου κρίνεται απαραίτητη για την αρχικοποίηση μη στατικών πεδίων, διότι σε περίπτωση ύπαρξης κληρονόμων της κλάσης μπορεί να υπάρξουν προβλήματα. | Προφανώς η μέθοδος μπορεί να επαναχρησιμοποιηθεί και σε άλλα σημεία του κώδικα της κλάσης. Η χρήση //final// μεθόδου κρίνεται απαραίτητη για την αρχικοποίηση μη στατικών πεδίων, διότι σε περίπτωση ύπαρξης κληρονόμων της κλάσης μπορεί να υπάρξουν προβλήματα. | ||
- | <code java Bicycle.java> | + | <code java Rectangle.java> |
- | public class Bicycle { | + | public class Rectangle { |
+ | |||
+ | // the Rectangle class has 3 fields | ||
+ | private int width; | ||
+ | private int height; | ||
+ | private Point origin; | ||
+ | |||
+ | private int id = initializeId(); | ||
- | private int cadence; | + | // add a class variable for the |
- | private int gear; | + | // number of Rectangle objects instantiated |
- | private int speed; | + | private static int numberOfRectangles = initNumberOfRectangles(); |
- | + | ||
- | private int id = initializeId(); | + | |
- | + | ||
- | private static int numberOfBicycles = initNumberOfBicycles(); | + | |
- | + | ||
- | public Bicycle(int startCadence, | + | |
- | int startSpeed, | + | |
- | int startGear){ | + | |
- | gear = startGear; | + | |
- | cadence = startCadence; | + | |
- | speed = startSpeed; | + | |
- | + | ||
- | id = ++numberOfBicycles; | + | |
- | } | + | |
- | + | ||
- | public int getID() { | + | |
- | return id; | + | |
- | } | + | |
| | ||
- | // static method for initializing static variable. | + | // the Rectangle class has one constructor |
- | private static int initNumberOfBicycles() { | + | public Rectangle(int setWidth, int setHeight, Point o) { |
- | System.out.println("initialize numberOfBicycles in static method"); | + | this(setWidth, setHeight); |
- | return 0; | + | origin = o; |
- | } | + | } |
+ | |||
+ | public Rectangle(int setWidth, int setHeight) { | ||
+ | width = setWidth; | ||
+ | height = setHeight; | ||
+ | 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 initialization block |
- | static { | + | static { |
- | System.out.println("initialize numberOfBicycles in static block"); | + | System.out.println("initialize numberOfRectangles in static block"); |
- | numberOfBicycles = 0; | + | numberOfRectangles = 0; |
- | } | + | } |
- | //non-static initialization block | + | //non-static initialization block |
- | { | + | { |
- | System.out.println("initialize objectId in block"); | + | id = 100; |
- | 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; | ||
+ | } | ||
- | //final method | + | public static int getNumberOfRectangles() { |
- | private final int initializeId() { | + | return numberOfRectangles; |
- | System.out.println("initialize objectId in final method"); | + | } |
- | return 200; | + | |
- | } | + | |
| | ||
- | public static int getNumberOfBicycles() { | + | // the Rectangel class has 3 methods |
- | return numberOfBicycles; | + | public void setWidth(int newWidth ) { |
- | } | + | width = newWidth; |
- | + | } | |
- | public String toString(String objName) { | + | |
- | return "["+objName+"."+id+"] Cadence: "+cadence+", Gear: "+gear+", Speed: "+speed; | + | public int getWidth() { |
- | } | + | return width; |
+ | } | ||
| | ||
- | public void printState(String objName) { | + | public void setHeight(int newHeight ) { |
- | System.out.println( toString(objName) ); | + | height = newHeight; |
- | } | + | } |
+ | |||
+ | public int getHeight() { | ||
+ | return height; | ||
+ | } | ||
| | ||
- | public static void main(String args[]) { | + | public void setOrigin(Point o) { |
- | Bicycle myBike = new Bicycle(1, 4, 2); | + | origin = o; |
- | Bicycle yourBike = new Bicycle(2, 3, 3); | + | } |
- | myBike.printState("myBike"); | + | |
- | yourBike.printState("yourBike"); | + | public Point getOrigin() { |
- | + | return origin; | |
- | System.out.println("Number of bicycles: "+ getNumberOfBicycles()); | + | } |
- | } | + | |
+ | 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> | ||
Line 151: | Line 190: | ||
Το αποτέλεσμα εκτέλεσης το παραπάνω κώδικα είναι το παρακάτω: | Το αποτέλεσμα εκτέλεσης το παραπάνω κώδικα είναι το παρακάτω: | ||
<code> | <code> | ||
- | initialize numberOfBicycles in static method | + | $ java Rectangle |
- | initialize numberOfBicycles in static block | + | initialize numberOfRectangles in static method |
- | initialize objectId in final method | + | initialize numberOfRectangles in static block |
- | initialize objectId in block | + | ------------------------------- |
- | initialize objectId in final method | + | initialize objectId in final method, id: 200 |
- | initialize objectId in block | + | initialize objectId in block, id: 100 |
- | [myBike.1] Cadence: 1, Gear: 2, Speed: 4 | + | Initialized object id in constructor, id:1 |
- | [yourBike.2] Cadence: 2, Gear: 3, Speed: 3 | + | [rectOne.1] Width: 30, Height: 40, Origin: [Point xPos:10, yPos:20] |
- | Number of bicycles: 2 | + | ------------------------------- |
+ | Number of Rectangles: 1 | ||
</code> | </code> | ||