One, the static application one:
For example, the Array tool class:
Each application has common features that can be extracted and packaged separately for reuse
Although you can use these tool methods to manipulate arrays by building Arraytool objects
But:
1. The object is used to encapsulate the data, but the Arraytool object does not encapsulate the unique data (no member variables).
2. Each method of manipulating an array does not use the unique data in the Arraytool object
At this time consider, let the program more rigorous, do not need the object, you can define the method in the Arraytool as static, directly with the class name call
It is easy to use a method after it is static, but the class can also be used by other programs to create objects
To be more rigorous, forcing the class to not be an object can be done by privatizing the constructor
Instructions for making a program: using Java document Annotations to create a Java document you must modify the class with public
How to compile the instructions: javadoc-d. (Represents the current directory, or you can specify a different directory (if it is not created automatically)-author-version Arraytool.java
1 /**2 This is a tool class that can operate on an array, providing functions such as getting the most value, sorting, etc.3 @authorHuang Jianfei4 @versionV1.15 */6 //tool class for manipulating arrays using the array tool7 Public classarraytool{8 /**9 Null parameter constructorTen */ One PrivateArraytool () {}//Each class has a default constructor, privatizing the constructor, forcing the object to be created A /** - gets the maximum value in the shaping array. - @paramArr accepts an array of type int the @returnreturns the maximum value in an array - */ - Public Static intGetmax (int[] arr) { - intMax=0; + for(intx=1;x<arr.length; x + + ){ - if(arr[x]>Arr[max]) +max=x; A } at returnArr[max]; - } - /** - gets the minimum value in the shaping array. - @paramArr accepts an array of type int - @returnreturns the smallest value in an array in */ - Public Static intGetmin (int[] arr) { to intMin=0; + for(intx=1;x<arr.length; x + + ){ - if(arr[x]<Arr[min]) themin=x; * } $ returnArr[min];Panax Notoginseng } - the /** + Select Sort int array A @paramArr accepts an array of type int the */ + Public Static voidSelectsort (int[] arr) { - for(intx=0; x<arr.length-1; x + + ){ $ for(intY=x+1;y<arr.length; y++ ){ $ if(arr[x]>Arr[y]) { - swap (arr,x,y); - } the } - }Wuyi } the - /** Wu to bubble sort an int array - @paramArr accepts an array of type int About */ $ Public Static voidBubblesort (int[] arr) { - for(intx=0;x<arr.length-1; x + + ){ - for(inty=0;y<arr.length-x-1; y++ ){ - if(arr[y]>arr[y+1]){ ASwap (arr,y,y+1); + } the } - } $ } the the the /** the permutation of the positions of elements in an array - @paramArr accepts an array of type int in @parama position to displace the @paramb The position to displace the */ About Private Static voidSwapint[] arr,intAintb) { the inttemp=Arr[a]; thearr[a]=Arr[b]; thearr[b]=temp; + } - the /**Bayi used to print the elements in an array. Printed in the form: [Element1,element2,......] the */ the Public Static voidPrintArray (int[] arr) { -System.out.print ("["); - for(intx=0;x<arr.length;x++) the { the if(x!=arr.length-1) theSystem.out.print (arr[x]+ ","); the Else -System.out.println (arr[x]+ "]"); the } the } the }94 //The permissions of the default constructor are changed as the class changes, that is, if the class is modified by public, the default constructor is also decorated with the public
1 Public classArraytooldemo {2 Public Static voidMain (string[] args) {3 int[] arr={3,3,4,5,6,7,68,34,9};4 intMax=arraytool.getmax (arr);//called directly with the class name5System.out.println ("max=" +max);6 }7}
Second, static application two:
There are a group of children playing snowman, and sometimes a new child to join, how to know how many people are playing
Please use the idea of facing objects, write programs to solve
1 Public classStatictest {2 Public Static voidMain (string[] args) {3Child D1 =NewChild (3, "Niuniu");4 d1.joingame ();5 6Child D2 =NewChild (2, "Meimei");7 d2.joingame ();8 9Child D3 =NewChild (5, "Yuanyuan");Ten d3.joingame (); OneSYSTEM.OUT.PRINTLN ("total =" +child.num); A } - } - classchild{ the intAge ; - String name; - Static intnum = 0; - + PublicChild (intage,string name) { - This. Age =Age ; + This. Name =name; A } at Public voidJoingame () { -num++; -System.out.println ("A child has joined"); - } -}
Javac *.java//can compile all the. java files in the current directory
Static Application and Documentation comments