.3. Kêu gọi một phương phápTrong việc tạo ra một phương pháp, bạn cung cấp cho một định nghĩa của những gì các phương pháp là để làm. Để sử dụng một phương pháp, bạn phải gọi điện thoại hoặc gọi nó. Có hai cách để gọi một phương pháp; sự lựa chọn ngày cho dù phương pháp trả về một giá trị hoặc không.Nếu phương pháp trả về một giá trị, một cuộc gọi đến phương pháp thường được coi là một giá trị. Ví dụ,int lớn hơn = tối (3, 4);cuộc gọi tối đa (3, 4) và đặt kết quả của phương pháp biến lớn hơn. Một ví dụ khác của một cuộc gọi được xử lý như một giá trị làSystem.out.println (tối đa (3, 4));mà in giá trị trả lại phương pháp gọi tối đa (3, 4).Nếu phương pháp trở về void, một cuộc gọi đến phương pháp phải là một tuyên bố. Ví dụ, println phương pháp trở về void. Các cuộc gọi là một tuyên bố:System.out.println ("Chào mừng đến với Java!");Lưu ý Một phương pháp với một loại giá trị trả lại nonvoid cũng có thể được kích hoạt như một tuyên bố trong Java. Trong trường hợp này, người gọi đơn giản là bỏ qua giá trị trả lại. Điều này là hiếm, nhưng cho phép nếu người gọi không phải là quan tâm đến giá trị trả lại. Khi một chương trình gọi là một phương pháp, chương trình điều khiển được chuyển giao cho các phương pháp được gọi là. Một phương pháp gọi là trả lại kiểm soát cho người gọi khi tuyên bố trở lại của nó được thực hiện hoặc khi phương pháp kết thúc đóng cửa đôi được đạt tới.Danh sách 5.1 cho thấy một chương trình đầy đủ được sử dụng để thử nghiệm các phương pháp tối đa. Đầu ra của chương trình được hiển thị trong hình 5.2.Con số 5,2. Chương trình invokes max (i, j) để có được giá trị tối đa giữa i và j.--------------------------------------------------------------------------------[Page 132]Listing 5.1. TestMax.java 1 public class TestMax { 2 /** Main method */ 3 public static void main(String[] args) { 4 int i = 5; 5 int j = 2; 6 int k = max(i, j); 7 System.out.println("The maximum between " + i + 8 " and " + j + " is " + k); 9 }1011 /** Return the max between two numbers */12 public static int max(int num1, int num2) {13 int result;1415 if (num1 > num2)16 result = num1;17 else18 result = num2;1920 return result; 21 }22 } This program contains the main method and the max method. The main method is just like any other method except that it is invoked by the JVM.The main method's header is always the same, like the one in this example, with the modifiers public and static, return value type void, method name main, and a parameter of the String[] type. String[] indicates that the parameter is an array of String, a subject addressed in Chapter 6, "Arrays."The statements in main may invoke other methods that are defined in the class that contains the main method or in other classes. In this example, the main method invokes max(i, j), which is defined in the same class with the main method.When the max method is invoked (line 6), variable i's value 5 is passed to num1, and variable j's value 2 is passed to num2 in the max method. The flow of control transfers to the max method. The max method is executed. When the return statement in the max method is executed, the max method returns the control to its caller (in this case the caller is the main method). This process is illustrated in Figure 5.3.Figure 5.3. When the max method is invoked, the flow of control transfers to the max method. Once the max method is finished, it returns the control back to the caller. --------------------------------------------------------------------------------[Page 133]Caution A return statement is required for a nonvoid method. The method shown below in (a) is logically correct, but it has a compilation error because the Java compiler thinks it possible that this method does not return any value. To fix this problem, delete if (n < 0) in (a), so that the compiler will see a return statement to be reached regardless of how the if statement is evaluated. Note One of the benefits of methods is for reuse. The max method can be invoked from any class besides TestMax. If you create a new class, Test, you can invoke the max method using ClassName.methodName (i.e., TestMax.max). 5.3.1. Call StacksEach time a method is invoked, the system stores parameters and variables in an area of memory, known as a stack, which stores elements in last-in first-out fashion. When a method calls another method, the caller's stack space is kept intact, and new space is created to handle the new method call. When a method finishes its work and returns to its caller, its associated space is released.
Understanding call stacks helps you to comprehend how methods are invoked. The variables defined in the main method are i, j, and k. The variables defined in the max method are num1, num2, and result. The variables num1 and num2 are defined in the method signature and are parameters of the method. Their values are passed through method invocation. Figure 5.4 illustrates the variables in the stack.
Figure 5.4. When the max method is invoked, the flow of control transfers to the max method. Once the max method is finished, it returns the control back to the caller.
Tip
If you use an IDE such as JBuilder, NetBeans, or Eclipse, please refer to Learning Java Effectively with JBuilder/NetBeans/Eclipse in the supplements. This supplement shows you how to use a debugger to trace method invocations.
đang được dịch, vui lòng đợi..
