Deeply in-access Java object-oriented preparatory articles (2. Research)

zhaozj2021-02-16  60

Java's Definition of Method: 1 Crepctor in Java can define a particular task used multiple times in a program as a method. These methods are sometimes becoming (programmers Custom): Programmer -defined method). 2 Method can be activated by the method call to perform other tasks. According to an example: the boss (caller) requires employees (called the caller) to complete a task, while the boss I don't know how workers have completed the task, and the organizers in the workers are allocated to other first-line workers, the method and its call are the same as the above-mentioned relationship. 3 method calls, the method makes the program The program can modularize the program, and the advantage is that the method of this division makes the software easier to manage, 2. Another advantage is "Software Reuse" means the existing method constitutes a new program, This is also a point that is a focus of object-oriented programming. 3. That is to avoid reuse of code in the program. Package the code to make the code through the calling method, and execute the code in several different locations in the program. Thus speeding up the program execution speed. The following uses an example can be called call. 1 //fig.1-1:squareint.java 2 Import java.awt.container; 3 Import javax.swing. *;

4 public class squareint extends japplet {5 public void {6 string output = "; 7 jtextarea outputarea = new jtextarea (10, 20); 8 Container C = getContentPane (); 9 C.Add (Outputarea);

10 int result; 11 for (int x = 1; x <= 10; x ) {12 results = Square (x); 13 OUTPUT = "The Square of" X "IS" Result "/ n"; 14} 15 OutputArea.setText (OUTPUT); 16} 17 Public INT Square (int y) 18 {RETURN Y * Y} 19} 20} This example is a calculation square applet, where loop control will 1-10 squares Applet's Container is displayed. We will gradually analyze the method calls: 1. Jtextarea outputarea = new jtextAREA (10, 20); Declare Jtextarea reference Outputarea, and assign a value JTextArea call results. Method Jtextarea OutputArea = New JtextArea (10, 20); Returns a content pane reference for an applet, you can display multi-line text GUI components. 2. The same line 8 Container C = getContentPane (); declares that Contanier reference C, assign Method C = getContentPane () call results. This method can return an applet's content pane reference, which can be used to display text boxes in the applet, and C.Add (Outputarea); put OutputArea JTextArea GUI components into the object applet Middle. Make Applets 3 Row OutputArea.Settext (OUTPUT); Use Method SetText Set OutputArea's Text to Output String 4 and the above three methods can be used in such languages: I now create an object called now OutputArea then displays a 10-line 20 column text box in Jtextarea, (destination), then I want to put it in the applet, then create the object C and c references the getContentPane in Container. () Method. (OK successfully) The text is then displayed in the use of the method setText. 5. Method Overload: method can define a few The same name method, as long as these methods have different parameter sets (parameter A number. Type. And order), this is called method overload. When the method is called, Java's compiler passes an inspection call. The number, type, and order of the parameters in the statement can choose the appropriate method. Method Overload is generally used to create similar operations for different types of data. The overload method is based on the combination of method names and parameter types, parameters The order is important to the compiler, the method cannot distinguish between the return value type, the method overload can have different return value types. But must have a different parameter table. For example, the method can be reloaded the Import Java AWT. *; import javax.swing *;. public class MethodOverload extends JApplet {JTextArea.outputArea; public void init () {outputArea = new JTextArea (2,20); Container c = get ContentPane (); c.add (outputArea);

Outputarea.Settext ("The Square of Integer 7 IS" Square (7) "/ NThe Square of Double 7.5 IS" Square (7.5));} public int square (int x) {return x * x;} PUBLIC Double Square (double y) {return y * y;} This program is based on the value returned in the program to determine the Return method. Summary: There are still many ways in Java, such as Integer.Parseint Joptionpane.ShowMessageDialog Most These methods reflect the Java's Upcasting Thought. So the method is called in the Java program design. The method's call is like a cooking, the API in Java is like a variety of vegetables. They are now Just in front of you, and now you only need to use these vegetables to use your own craft (method) to give us a rich dinner (program). Don't need you to know how this dish is coming, How to make a dish, how to match it. Everything is watching you.

Note: Common errors in the middle of the JAVA: 1. In the definition of the method, the non-written return value type generates a syntax error 2. Returns a value from a return type as a void method to generate a syntax error 3. In the method Add a semicolon of the right brackets of the parameter table. A semicolon is an error. 4. Parameter of the parameter to the method is incompatible with the corresponding parameter type is syntax error 5. Defining another method in one method will generate syntax errors

转载请注明原文地址:https://www.9cbs.com/read-22873.html

New Post(0)