我在试着理解为什么这个不能运行。我在想,我可能需要导入一个包。我还想知道在LocalVariables类的myObject.f()行上发生了什么;
我想我刚刚在前一行实例化了myObject,但是我是用myObject.f()调用f方法吗?我不明白在这条线上应该发生什么。
任何帮助都将不胜感激。
class MyObject{
static short s = 400; //static variable
int i = 200; //instance variable
void f() {
System.out.println("s = " + s);
System.out.println("i = " + i);
short s = 300; //local variable
int i = 100; //local variable
double d = 1E100; //local variable
System.out.println("s = "+s);
System.out.println("i = " +i);
System.out.println("d = " + d);
}
}
class LocalVariables{
public static void main(String[] args){
MyObject myObject = new MyObject();
myObject.f();
}
}发布于 2010-09-30 23:47:03
change void f() to public void f() { //Code... }发布于 2010-09-30 23:40:23
是的,您正在调用f()方法。因此,控制流将跳转到f()函数的顶部,并逐个执行这些语句。
一旦完成,它将返回到main方法,从它停止的地方继续。这在这里没有任何意义,因为myObject.f()是最后一行,但是如果您有更多的代码,那么一旦f()方法返回,就会执行这些代码。
发布于 2010-09-30 23:42:04
好的,执行这段代码应该已经打印了几行
s = 400
i = 200
s = 300
i = 100
d = <something>您的输出中是否出现过这种类型的内容?
https://stackoverflow.com/questions/3832210
复制相似问题