java exercises - (static)

1.

Frock declared in the class private static property currentNum, the initial value of 100,000, as the garment factory SEQ ID start value.

The statement public static method getNextNum, as a method of generating a unique serial number of the shirt. Each call will currentNum increased by 100, and as a return value.

In TestFrock class main method, call getNextNum method twice to obtain the serial number and the printed output.

 

2.

Frock serialNumber attribute declared in the class, and to provide a corresponding get method;

In the constructor for class Frock, gets a unique serial number Frock getNextNum object by calling method;

In TestFrock class main method, Frock create three objects and three object print the serial number, verifies if incremented by 100.

 

3.

Frock static class declaration statement block, the initial value is set in the statement block currentNum 150,000, as starting value garment factory serial number, and print out the value.

Execute the main method TestFrock class, create three objects Frock respectively, to verify whether the static block of statements is executed only if once, and the serial number starting value has been adjusted.

 

4.

Public static constants in the statement INCREMENT Frock class value of 100, the code modification getNextNum static method, instead of the original 100 by INCREMENT constant incremental value.

the main method of execution TestFrock class, to verify whether the changes are correct.

 

Frock.java

Package static1; 

public  class Frock {
     Private  static  int currentNum; // appearances starting sequence number variable 
    Private  int the serialNumber;
     public   static  Final  int the INCREMENT = 100 ; 
    
    public  static  int getNextNum () {
         int TEMP = currentNum; 
        currentNum + = the INCREMENT;
         return TEMP; 
    } 
    
    static { 
        currentNum = 150,000; // appearances SEQ ID start value is 150000
        System.out.println ( "garment factory SEQ ID start value of:" + currentNum); 
    } 
    
    
    public Frock () {
         Super ();
         the this .serialNumber = getNextNum (); 
    } 




    public  int getSerialNumber () {
         return the serialNumber; 
    } 
    
}

 

TestFrock.java

package static1;

public class TestFrock {
    public static void main(String[] args) {
        Frock f1 = new Frock();
        Frock f2 = new Frock();
        Frock f3 = new Frock();
        System.out.println(f1.getSerialNumber());
        System.out.println(f2.getSerialNumber());
        System.out.println(f3.getSerialNumber());
    }
}

 

The result: 
clothes factory serial number starting value is: 150000 
150000 
150100 
150200

 

Guess you like

Origin www.cnblogs.com/shellxx/p/12484517.html