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
Next revision Both sides next revision
java:member_initialization [2017/02/10 09:21]
gthanos [Παράδειγμα αρχικοποίησης στατικών και μη στατικών πεδίων]
java:member_initialization [2020/02/21 12:28]
gthanos [Παράδειγμα αρχικοποίησης στατικών και μη στατικών πεδίων]
Line 52: Line 52:
 </code> </code>
  
-Το πλεονέκτημα των στατικών μεθόδων είναι ότι μπορούν να επαναχρησιμοποιηθούν σε περίπτωση που χρειαστεί να επανα-αρχικοποιηθεί η στατική μεταβλητή της κλάσης.+<WRAP tip 80% center round> 
 +Το πλεονέκτημα των στατικών μεθόδων είναι ότι μπορούν να επαναχρησιμοποιηθούν σε περίπτωση που χρειαστεί να αρχικοποιηθεί ξανά η στατική μεταβλητή της κλάσης. 
 +</WRAP>
  
 ===== Αρχικοποιώντας μη-στατικά πεδία ===== ===== Αρχικοποιώντας μη-στατικά πεδία =====
Line 78: Line 80:
  
 ===== Παράδειγμα αρχικοποίησης στατικών και μη στατικών πεδίων ===== ===== Παράδειγμα αρχικοποίησης στατικών και μη στατικών πεδίων =====
- 
  
 <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;
      
-  private int id = initializeId();+  int id = initializeId();
    
   // add a class variable for the   // add a class variable for the
   // number of Rectangle objects instantiated   // number of Rectangle objects instantiated
-  private static int numberOfRectangles = initNumberOfRectangles();+  static int numberOfRectangles = initNumberOfRectangles();
          
-  public Rectangle(int initWidth, int initHeight, Point initOrigin) {+  Rectangle(int initWidth, int initHeight, Point initOrigin) {
     this(initWidth, initHeight);     this(initWidth, initHeight);
     origin = initOrigin;     origin = initOrigin;
   }   }
      
-  public Rectangle(int initWidth, int initHeight) {+  Rectangle(int initWidth, int initHeight) {
     width = initWidth;     width = initWidth;
     height = initHeight;     height = initHeight;
Line 109: Line 110:
      
   // static method for initializing static variable.   // static method for initializing static variable.
-  private static int initNumberOfRectangles() {+  static int initNumberOfRectangles() {
     System.out.println("initialize numberOfRectangles in static method");     System.out.println("initialize numberOfRectangles in static method");
     return 0;     return 0;
Line 127: Line 128:
      
   //final method   //final method
-  private final int initializeId() {+  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  );
Line 133: Line 134:
   }   }
      
-  public int getID() {+  int getID() {
     return id;     return id;
   }   }
    
-  public static int getNumberOfRectangles() {+  static int getNumberOfRectangles() {
     return numberOfRectangles;     return numberOfRectangles;
   }   }
          
-  public void setWidth(int newWidth ) {+  void setWidth(int newWidth ) {
     width = newWidth;     width = newWidth;
   }   }
      
-  public int getWidth() {+  int getWidth() {
     return width;     return width;
   }   }
          
-  public void setHeight(int newHeight ) {+  void setHeight(int newHeight ) {
     height = newHeight;     height = newHeight;
   }   }
      
-  public int getHeight() {+  int getHeight() {
     return height;     return height;
   }   }
          
-  public void setOrigin(Point newOrigin) {+  void setOrigin(Point newOrigin) {
     origin = newOrigin;     origin = newOrigin;
   }   }
      
-  public Point getOrigin() {+  Point getOrigin() {
     return origin;     return origin;
   }   }
      
-  public int getArea() {+  int getArea() {
        return width * height;        return width * height;
   }   }
        
   // Move rectangle origin by x,y   // Move rectangle origin by x,y
-  public void move(int dx, int dy) {+  void move(int dx, int dy) {
     origin.setX( origin.getX() + dx );     origin.setX( origin.getX() + dx );
     origin.setY( origin.getY() + dy );     origin.setY( origin.getY() + dy );
   }   }
      
-  public String toString(String objName) {+  String toString(String objName) {
       return "["+objName+"." + id + "] Width: " + width + ", Height: " + height + ", Origin: " + origin;       return "["+objName+"." + id + "] Width: " + width + ", Height: " + height + ", Origin: " + origin;
   }   }
      
-  public static void main(String []args) {+  static void main(String []args) {
     System.out.println("-------------------------------");     System.out.println("-------------------------------");
     Point p = new Point(10,20);     Point p = new Point(10,20);
java/member_initialization.txt · Last modified: 2022/02/24 12:39 (external edit)