public class Test1 {
public static void main(String[]args){
int medal_all=800; //member variable
public void china(){
int medal_china=100; //local variable for the method
if(true){ //code block
int gold=50; //local variable for the code block
medal_china+=50; //to be allowed access
medal_all-=150; //to be allowed access
}
gold=100; //compile errors
medal_china+=100;//to be allowed access
medal_all-=200; //to be allowed access
public void(){
medal_all=800;//to be allowed access
medal_china=100;//compile errors,it can't allow to local variable of other method
gold=10; //compile errors
}
}
}
}
medal_china 和gold变量是china()方法体的局部变量,只有通过类的实例访问
gold是在if判断中定义的局部变量 在if之外是无法使用的
谢谢你们的回答,不是我要的。要不你们运行一下程序看看
要不,编译一下看看?
对不起,刚开始的变量前后不一致,是想一致的变量。
在方法体内定义方法,你是可以的
Main方法中又定义了一个方法,后面还有一个方法名字没写
java中方法体内不能定义方法,调用方法必须通过类