Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để chuyển cuộc gọi phương thức dưới dạng đối số cho một phương thức khác

Trong bài này, chúng ta sẽ hiểu cách chuyển lời gọi phương thức dưới dạng đối số cho một phương thức khác. Chúng ta có thể gọi một phương thức từ một lớp khác bằng cách tạo một đối tượng của lớp đó bên trong một lớp khác. Sau khi tạo một đối tượng, hãy gọi các phương thức bằng cách sử dụng biến tham chiếu đối tượng.

Dưới đây là một minh chứng về điều tương tự -

Đầu vào

Giả sử đầu vào của chúng tôi là -

Enter two numbers : 2 and 3

Đầu ra

Đầu ra mong muốn sẽ là -

The cube of the sum of two numbers is:
125

Thuật toán

Step 1 - START
Step 2 - Declare two variables values namely my_input_1 and my_input_2
Step 3 - We define a function that takes two numbers, and returns their sum.
Step 4 - We define another function that takes one argument and multiplies it thrice, and returns the output.
Step 5 - In the main function, we create a new object of the class, and create a Scanner object.
Step 6 - Now, we can either pre-define the number or prompt the user to enter it.
Step 7 - Once we have the inputs in place, we invoke the function that returns the cube of the input.
Step 8 - This result is displayed on the console.

Ví dụ 1

Ở đây, đầu vào đang được người dùng nhập dựa trên lời nhắc. Bạn có thể thử trực tiếp ví dụ này trong công cụ nền tảng mã hóa của chúng tôi Chương trình Java để chuyển cuộc gọi phương thức dưới dạng đối số cho một phương thức khác .

import java.util.Scanner;
public class Main {
   public int my_sum(int a, int b) {
      int sum = a + b;
      return sum;
   }
   public void my_cube(int my_input) {
      int my_result = my_input * my_input * my_input;
      System.out.println(my_result);
   }
   public static void main(String[] args) {
      Main obj = new Main();
      int my_input_1, my_input_2;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the first number : ");
      my_input_1 = my_scanner.nextInt();
      System.out.print("Enter the second number : ");
      my_input_2 = my_scanner.nextInt();
      System.out.println("The cube of the sum of two numbers is: ");
      obj.my_cube(obj.my_sum(my_input_1, my_input_2));
   }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the first number : 2
Enter the second number : 3
The cube of the sum of two numbers is:
125

Ví dụ 2

Ở đây, số nguyên đã được xác định trước đó và giá trị của nó được truy cập và hiển thị trên bảng điều khiển.

public class Main {
   public int my_sum(int a, int b) {
      int sum = a + b;
      return sum;
   }
   public void my_cube(int my_input) {
      int my_result = my_input * my_input * my_input;
      System.out.println(my_result);
   }
   public static void main(String[] args) {
      Main obj = new Main();
      int my_input_1, my_input_2;
      my_input_1 = 3;
      my_input_2 = 2;
      System.out.println("The two number is defined as " +my_input_1 +" and " +my_input_2);
      System.out.println("The cube of the sum of two numbers is: ");
      obj.my_cube(obj.my_sum(my_input_1, my_input_2));
   }
}

Đầu ra

The two number is defined as 3 and 2
The cube of the sum of two numbers is:
125