繁体   English   中英

如何使两个函数相互调用Java

[英]How to have two functions that call each other Java

我知道我们在C ++中是这样的:

int a();
int b() { 
  return a();
}
int a() { 
  return b();
}

我如何在Java中做类似的事情?

在Java中,您不必在使用变量或函数之前先声明它们。 因此:

int b() { return a();}
int a() { return b();}

请注意,这将产生StackOverflowError

无需前向声明,只需编写函数。

这是:对您来说危险的代码:

public class b 
{

   Object  first()
   {
     System.out.println("i am inside first function");
     return second();
   }

   Object  second()
   {
     System.out.println(" Like i care !  i'm scared of StackOverflowError dude !!");
     return first();
   }

   public static void main(String [] args)
   {
     new b().first();
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM