Java-Preliminary Understanding-eighth-array tool object creation

Source: Internet
Author: User

One.

I've finished talking about some of the features and precautions of static keywords, as well as explaining the static application scenarios. Now you are ready to write a concrete example of the previous knowledge points strung together.

This example is worn from the very beginning.

Requirements: Find the maximum value of the array

At first, our writing is to write the code in the main function, run it. Here are the methods we used to solve the problem before we learned.

(each sentence in the code has its meaning, and the statement without meaning and effect can be used without writing.) public static void Main (string[] args) This sentence is to ensure that the program runs independently, but this class does not need to run independently, so there is no main function in this class. )

Dos a run the above program, the need to solve. But let's say we don't write code like that anymore. The main function is just the portal and is used to invoke other programs. So, what should I write here?

The maximum value in the program can only solve the maximum values of an array, the array arr, and the for statement in X<arr.length are combined. If there is another array, rebuild a new code that solves the maximum value. This is not appropriate, the repetition of the writing basically the same code.

In order to improve the reusability of the code, it is necessary to encapsulate the function. (This is the function we talked about later.) The beginning is an array, and then the array is derived from a series of operations, followed by the function encapsulation function, before the time, only the main function one, all the code is placed in the main function. Now that the code is encapsulated and reused, we can find many arrays for maximum value. Just give us the array and we can do it.

At this point, define a function that gets the maximum value of an integer array.

The following program is unfolded, public is a modifier, indicating the maximum access rights (not quite understand?). Self-understanding). The existence of static is because the main function in this class is also static decorated, the main function to call this method, must be static modified (?), the Getmax method parameter in arr and the main function of ARR is not the same thing, but the same name. The function function finally returns the maximum value. The requirement is to get the maximum value, and the return value is the maximum value in the array.

The program after the encapsulation function is written as follows, and the contents of the main function are greatly reduced. So far, my understanding of the main function is that the main function is like a control panel with a few buttons on it. The complex work behind each button has been hidden from the shadows.

The appearance of functions improves reuse. Then you expand your mind, and in the other class, you need to solve the maximum value (which is not solved in a class). And it's not about solving the most value, sorting, binary. All needed, which means there are a lot of ways, what should we do now?

According to the idea of the object. Instead of defining them as methods, we should put them in a class, in other words, encapsulate them inside the object. I found the object that operates the array , did you find the function of the array of operations ? This is a transition, from the earliest main function, to the transition to our custom functions to encapsulate, and then transition to the many functions we need to do the object ( then this object is all methods?). How do I write the argument list for an object? ) for encapsulation. We are no longer oriented towards the process, but object oriented. Therefore, we have written a class here alone. After this class is finished, I can use it, and you can use it.

In the second method, there is a special place where the IF statement should be the position permutation, position substitution is a function, in this write a function is finished. This development way a lot of see, because our thinking is built in my this piece definitely need a function, we do not have to control how to achieve, first write this function, and then to fine-understand how to achieve this function.

Swap function is for selectsort service, do not need to provide out, so with private decoration. The parameter list for swap is three, give an array, and give two variables (that is, the two-point mark). Because we're replacing two of the two positions in the array, is the array clear? The array is ambiguous. How do you call the Swap method here? In one of the statements of the Selectsort method, the swap method is called. First there is a definite array called the Selecesort method, in the process of Selectsort method is executed, the Selectsort method calls the Swap method, the specific call is written as swap (arr, x, y); The return value is not required here.

In a simple look, here's what return-1 mean, forget .

Here is also a function of the printing array, the array into a string should be a method, the return is a string, where the simplest is a string connection, we all know that any data and character bed added, want to connect.   String str= ""; Is it null? This is a real string, but there is no content in it.

(The first is to set all the methods in a class, inside the method, there will also be a connection)

At this point, the function is finished, encapsulated in an object called Arraytool inside, called the Array tool object. Then combining the previous class, we want to get the maximum value of an array.

after the object has been written, what do we want to ask for the most value?

Here is actually two Java files, one is Arraytool.java, and the other is Arraytooldemo.java. The Arraytooldemo.java file contains the main function, Arraytooldemo is written by someone else, ready to use our tools. Two files, who should I compile first? Logically, the first compile Arraytool,arraytooldemo is in the use of Arraytool. If there is no Arraytool.class file, how to use it

The order of execution in DOS is expressed.

Then some changes were made, as follows. This class has changed, and the tool class has not changed.

Then the interesting thing came, first compiled Arraytooldemo.java, a compilation, two classes are out.

parsing: Arraytooledemo It does use Arraytool.class in this class, but does Arraytool.class have it? No, the Java compiler is very interesting, it found that the class used in other classes, it will go to the classpath path to find this class, or go to the current directory to find this class, a find no, it will continue to find, what to find? Find the. java file with the same class name. Because this class usually corresponds to a source file, find the source file, compile it, and then generate the class file,

Java-Preliminary Understanding-eighth-array tool object creation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.