要了解它背后的逻辑

时间:2010-12-28 12:21:59

标签: java

有没有人向我解释这个编码

public class FunctionCall {

  public static void funct1 () {
    System.out.println ("Inside funct1");
  }

  public static void main (String[] args) {
    int val;
    System.out.println ("Inside main");
    funct1();
    System.out.println ("About to call funct2");
    val = funct2(8);
    System.out.println ("funct2 returned a value of " + val);
    System.out.println ("About to call funct2 again");
    val = funct2(-3);
    System.out.println ("funct2 returned a value of " + val);
  }

  public static int funct2 (int param) {
    System.out.println ("Inside funct2 with param " + param);
    return param * 2;
  }
}

2 个答案:

答案 0 :(得分:3)

public class FunctionCall {//a public class

  public static void funct1 () { /a static method which returns nothing void
    System.out.println ("Inside funct1");
  }

  public static void main (String[] args) {//main method
    int val;//int variable declaration
    System.out.println ("Inside main");//printing on console
    funct1();//calling static method
    System.out.println ("About to call funct2");////printing on console
    val = funct2(8);//calling static method which returns int and assigning it to val variable
    System.out.println ("funct2 returned a value of " + val);//printing on console
    System.out.println ("About to call funct2 again");//printing on console
    val = funct2(-3);////calling static method which returns int and assigning it to val 
    System.out.println ("funct2 returned a value of " + val);//printing on console
  }

  public static int funct2 (int param) {
    System.out.println ("Inside funct2 with param " + param);
    return param * 2;
  }
}

答案 1 :(得分:0)

当然,问题出在哪里?

有2种静态方法

public static void funct1 () {
  System.out.println ("Inside funct1"); 
}

public static int funct2 (int param) {
  System.out.println ("Inside funct2 with param " + param); 
  return param * 2; 
}

控制台上的输出将是(如果我没有计算错误):

Inside main
Inside funct1
About to call funct2
Inside funct2 with param 8
funct2 returned a value of 16
About to call funct2 again
Inside funct2 with param -3
funct2 returned a value of -6

从main开始,然后进入funct1一次;然后它用8调用func2,用-3

调用另一个时间
相关问题