[Java] Java 实例变量 局部变量 →→→→→进入此内容的聊天室

来自 , 2019-06-11, 写在 Java, 查看 213 次.
URL http://www.code666.cn/view/7e7e69ea
  1. public class Test {
  2. private int i; //
  3. Test类的实例变量
  4. public int firstMethod() {
  5. int j = 1; //
  6. 局部变量
  7. // 这里能够访问i和j
  8. System.out.println("firstMethod
  9. 中
  10. i="+i+",j="+j);
  11. return 1;
  12. } // firstMethod()
  13. 方法结束
  14. public int secondMethod(float f) { //method parameter
  15. int j = 2; //局部变量,跟firstMethod()
  16. 方法中的j是不同的
  17. //
  18. 这个j的范围是限制
  19. 在secondMethod()
  20. 种的
  21. //
  22. 在这个地方,可以
  23. 同时访问i,j,f
  24. System.out.println("secondMethod
  25. 中
  26. i="+i+",j="+j+",f="+f);
  27. return 2;
  28. }
  29. public static void main(String[] args) {
  30. Test t = new Test();
  31. t.firstMethod();
  32. t.secondMethod(3);
  33. }
  34. }//源代码片段来自云代码http://yuncode.net
  35.                        

回复 "Java 实例变量 局部变量"

这儿你可以回复上面这条便签

captcha