If you have multiple objects that use the same function or multiple functions, define a tool class before calling
classarraytool{ Public intGetmax (int[] arr) { intMax=0; for(intx=1;x<arr.length;x++) { if(arr[x]>Arr[max]) Max=x; } returnArr[max]; } Public intGetmin (int[] arr) { intMin=0; for(intx=1;x<arr.length;x++) { if(arr[x]<arr[min]) min=x; } returnArr[min]; } Public 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); } } } Public 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]) Swap (Arr,y,y+1); } } } Public voidSwapint[]arr,intAintb) {inttemp=Arr[a]; Arr[a]=Arr[b]; ARR[B]=temp; }}
Public class Hello { publicstaticvoid main (string[] args) { int[] arr={3,4,1,8}; Arraytool tool=new arraytool (); int max=Tool.getmax (arr); System.out.println ("max=" +max);} }
Although it is possible to call the batch tool array by establishing an Arraytool object, the problem is found
1. Objects are used to encapsulate data, but the Arraytool object does not encapsulate the holding data
2. Each method that operates the verse does not use the unique data in the Arraytool object
At this point, consider that you do not need objects
All methods in Arraytool can be defined as static and are called directly from the class name
However, it is also possible to have its new object privatized by constructing objects, blocking the creation of objects, and functions such as swap that do not need to be exposed to the outside world
classarraytool{
Private Arraytool () {} Public Static intGetmax (int[] arr) { intMax=0; for(intx=1;x<arr.length;x++) { if(arr[x]>Arr[max]) Max=x; } returnArr[max]; } Public Static intGetmin (int[] arr) { intMin=0; for(intx=1;x<arr.length;x++) { if(arr[x]<arr[min]) min=x; } returnArr[min]; } 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); } } } 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]) Swap (Arr,y,y+1); } } } Private Static voidSwapint[]arr,intAintb) {inttemp=Arr[a]; Arr[a]=Arr[b]; ARR[B]=temp; }}
Public class Arraytooldemo { publicstaticvoid main (string[] args) { int [] arr={3,4,1,8}; int max=Arraytool.getmax (arr); System.out.println ("max=" +max);} }
In a class, there is a constructor for an empty argument by default;
This default constructor has the same permissions as the owning class (public or no);
If the class is modified by public, the default constructor is also with the public modifier;
If the class is not modified by public, the default constructor does not have a public adornment.
Execution order
Person p=new person (parameter);
class loading, static code block loading, property (non-static) loading, default initialization, display initialization (direct assignment), building code block loading, constructor initialization
Java Object-oriented encapsulation