直接po代码案例


运行结果如下:

完整的代码如下:
package test;
public class Test {
int a = 5;
int b = 10;
int c = 200;
int d = 500;
int e = 800;
public int functionA() {
System.out.println("操作变量a之前,a = " + a);
a++;
System.out.println("操作变量a之后,a = " + a);
return a;
}
public int functionB() {
System.out.println("操作变量b之前,b = " + b);
++b;
System.out.println("操作变量b之后,b = " + b);
return b;
}
public int functionC() {
System.out.println("操作变量c之前,c = " + c);
return c++; // 后++(先返回c,然后c再自加)
}
public int functionD() {
System.out.println("操作变量d之前,d = " + d);
return ++d; // 前++(d先自加,然后再把d返回)
}
public int functionE() {
System.out.println("操作变量e之前,e = " + e);
e = e + 66;
System.out.println("操作变量e之后,e = " + e);
return e;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test = new Test();
System.out.println("functionA函数的返回值是" + test.functionA() + ",调用完函数后a = " + test.a);
System.out.println("-----------\n");
System.out.println("functionB函数的返回值是" + test.functionB() + ",调用完函数后b = " + test.b);
System.out.println("-----------\n");
System.out.println("functionC函数的返回值是" + test.functionC() + ",调用完函数后c = " + test.c);
System.out.println("-----------\n");
System.out.println("functionD函数的返回值是" + test.functionD() + ",调用完函数后d = " + test.d);
System.out.println("-----------\n");
System.out.println("functionE函数的返回值是" + test.functionE() + ",调用完函数后e = " + test.e);
}
}
文章详细描述了一个Java类Test中的五个公共方法,展示了如何在函数中操作变量(如自增),以及每个函数的返回值行为。通过main方法调用这些函数并输出结果,以说明程序执行过程。
866

被折叠的 条评论
为什么被折叠?



