显示方法嵌套的Java程序
在Java中,我们在一个类中创建的方法和变量只能通过该类的对象来调用,如果是静态方法,我们可以直接使用类名来调用它。可以在点运算符的帮助下调用方法和变量。但是有一种特殊情况,一个方法也可以在类名的帮助下被另一个方法调用,但条件是它们应该存在于同一个类中。
句法:
class Main
{
method1(){
// statements
}
method2()
{
// statements
// calling method1() from method2()
method1();
}
method3()
{
// statements
// calling of method2() from method3()
method2();
}
}
示例 1:
在以下示例中,该类包含两个方法,即 Area() 和 Volume(),它们用于计算球体的面积和体积。 main()方法调用area()方法计算球体的面积,调用volume()方法计算球体的体积,然后显示结果。
Java
// Java Program implementing the Nesting of Methods
public class NestingMethodCalls1 {
public void Area(double r)
{
// value of r is coming from main method from which
// this method is called Math.pi is used for getting
// the value of pi
System.out.println(
"****** Inside Area method ******");
// calculate area
double a = 4 * Math.PI * r * r;
// print the area
System.out.println("Surface area of a Sphere is : "
+ a);
}
public void Volume(double r)
{
// value of r is coming from main method from which
// this method is called Math.pi is used for getting
// the value of pi
System.out.println(
"****** Inside Volume method ******");
// calculate the volume
double v = (4 / 3) * Math.PI * r * r * r;
// print the volume
System.out.println("Volume of a Sphere is : " + v);
}
public static void main(String args[])
{
// creating object of class GeeksforGeeks
GeeksforGeeks gfg = new GeeksforGeeks();
// calling of method Area() from main method
gfg.Area(12);
// calling of method Volume() from main method
gfg.Volume(12);
}
}
Java
// Java Program implementing the Nesting of Methods
public class NestingMethodCalls2 {
public void swap(int x, int y)
{
System.out.println(
"*********This is swap method***********");
System.out.println("Before swapping:x=" + x + " "
+ "y=" + y);
// swap the numbers
int z = x;
x = y;
y = z;
System.out.println("After Swapping:a=" + x + " "
+ "b=" + y);
}
public void gfg1(int a, int b)
{
System.out.println(
"*********This is gfg1 method***********");
System.out.println("Before performing operation:a="
+ a + " "
+ "b=" + b);
a = a + 10;
b = b + 12;
System.out.println("After operation:a=" + a + " "
+ "b=" + b);
// calling of swap() method from gfg1() method
swap(a, b);
}
public static void main(String args[])
{
// creating the object of class GeeksforGeeks2
GeeksforGeeks2 gfg2 = new GeeksforGeeks2();
int a = 20, b = 30;
// calling of method gfg1() from main method
gfg2.gfg1(a, b);
}
}
Java
// Java Program implementing the Nesting of Methods
public class NestingMethodCalls3 {
public void a1(int a, int b)
{
a = a + 10;
b = b + 20;
System.out.println(
"****** Inside a1 method ******");
System.out.println("a = " + a + " "
+ "b = " + b);
// calling method a2() from a1() with parameters a
// and b
a2(a, b);
}
public void a2(int x, int y)
{
x = x + 100;
y = y + 200;
System.out.println(
"****** Inside a2 method ******");
System.out.println("x = " + x + " "
+ "y = " + y);
}
public void a3(int w, int z)
{
w = w + 50;
z = z - 50;
System.out.println(
"****** Inside a3 method ******");
System.out.println("w = " + w + " "
+ "z = " + z);
// calling method a1() from method a3() with
// parameters w and z
a1(w, z);
}
public static void main(String[] args)
{
// creating the object of class GeeksforGeeks3()
GeeksforGeeks3 gfg3 = new GeeksforGeeks3();
int a = 100, b = 200;
// calling method a3() from main() method
gfg3.a3(a, b);
}
}
输出
****** Inside Area method ******
Surface area of a Sphere is : 1809.5573684677208
****** Inside Volume method ******
Volume of a Sphere is : 5428.672105403162
示例 2:
在以下示例中,该类包含两个方法,即 swap() 和 gfg1()。 swap() 方法用于交换两个变量,gfg1() 方法用于将两个数字相加。 main() 方法使用两个值调用 gfg1() 方法来添加两个数字,gfg1() 方法调用 swap() 方法来执行两个数字的交换。
Java
// Java Program implementing the Nesting of Methods
public class NestingMethodCalls2 {
public void swap(int x, int y)
{
System.out.println(
"*********This is swap method***********");
System.out.println("Before swapping:x=" + x + " "
+ "y=" + y);
// swap the numbers
int z = x;
x = y;
y = z;
System.out.println("After Swapping:a=" + x + " "
+ "b=" + y);
}
public void gfg1(int a, int b)
{
System.out.println(
"*********This is gfg1 method***********");
System.out.println("Before performing operation:a="
+ a + " "
+ "b=" + b);
a = a + 10;
b = b + 12;
System.out.println("After operation:a=" + a + " "
+ "b=" + b);
// calling of swap() method from gfg1() method
swap(a, b);
}
public static void main(String args[])
{
// creating the object of class GeeksforGeeks2
GeeksforGeeks2 gfg2 = new GeeksforGeeks2();
int a = 20, b = 30;
// calling of method gfg1() from main method
gfg2.gfg1(a, b);
}
}
输出
*********This is gfg1 method***********
Before performing operation:a=20 b=30
After operation:a=30 b=42
*********This is swap method***********
Before swapping:x=30 y=42
After Swapping:a=42 b=30
示例 3:
在以下示例中,该类包含三个方法,即 a1()、a2()、a3()。 main() 方法调用带有值 a 和 b 的 a3() 方法,然后 a2() 调用另一个方法 a1() 然后这个方法执行他的所有操作,然后将它们的值传递给另一个方法意味着这个方法然后调用另一个方法,即 a2() 然后这将执行,然后程序终止。
Java
// Java Program implementing the Nesting of Methods
public class NestingMethodCalls3 {
public void a1(int a, int b)
{
a = a + 10;
b = b + 20;
System.out.println(
"****** Inside a1 method ******");
System.out.println("a = " + a + " "
+ "b = " + b);
// calling method a2() from a1() with parameters a
// and b
a2(a, b);
}
public void a2(int x, int y)
{
x = x + 100;
y = y + 200;
System.out.println(
"****** Inside a2 method ******");
System.out.println("x = " + x + " "
+ "y = " + y);
}
public void a3(int w, int z)
{
w = w + 50;
z = z - 50;
System.out.println(
"****** Inside a3 method ******");
System.out.println("w = " + w + " "
+ "z = " + z);
// calling method a1() from method a3() with
// parameters w and z
a1(w, z);
}
public static void main(String[] args)
{
// creating the object of class GeeksforGeeks3()
GeeksforGeeks3 gfg3 = new GeeksforGeeks3();
int a = 100, b = 200;
// calling method a3() from main() method
gfg3.a3(a, b);
}
}
输出
****** Inside a3 method ******
w = 150 z = 150
****** Inside a1 method ******
a = 160 b = 170
****** Inside a2 method ******
x = 260 y = 370