执行此代码的结果是什么?为什么? [英] What's the result of executing this code and why .

查看:63
本文介绍了执行此代码的结果是什么?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function test ()
{
console.log(a);
console.log(foo());
var a = 1;
function foo ()
{
 return 2;
}
}
test();





我的尝试:



没什么,因为上面的代码甚至没有编译。



What I have tried:

Nothing, because the above code doesn't even compile.

推荐答案

因为它看来,你提出的程序只是(不正确的)伪代码。正如已经建议的那样,你真的需要阅读一个 Java 教程,以便至少掌握这种编程语言的基础知识。

下面一个根据你的伪代码工作 Java 程序。

As it stands, the program you proposed is just (incorrect) pseudo code. You really need, as already suggested, to read a Java tutorial to grasp, at least, the rudimentals of such a programming language.
Below a working Java program, based on your pseudo code.
class FooTest // you need at least a class, in a Java program
{
  int a;

  public FooTest(int a){this.a = a;} // class ctor

  public void test () // this is an 'instance method' of the class
  {
    int a = 1;
    System.out.println(a);
    System.out.println(FooTest.foo()); // note you don't need an instance of the class in order to call the 'foo' static method
  }
  public static int foo () // this is a 'static method' of the class
  {
    return 2;
  }

  public static void main ( String args[]) // the main function is the required program entry point
  {
    FooTest ft = new FooTest(42); // you need an instance of the class in order to call the test method
    ft.test();
  }
}


无法执行代码是什么原因
am unable to execute the code what is reason


这篇关于执行此代码的结果是什么?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆