通过两个类StaticDemo、Main 说明静态变量/方法与实例变量/方法的区别。 以下程序编译的时候会报错,请修改程序,使得程序能够正确运行并输出和样例一样的结果。 注意不要改变那几行println的顺序。
class StaticDemo {static int x;// 1)int y;public static int getX() {// 2}return x;}public static void setX(int newX) {x = newX;}public int getY() {return y;}public void setY(int newY) {y = newY;}}public class Main {public static void main(String[] args) {System.out.println("静态变量x=" + StaticDemo.getX());System.out.println("实例变量y=" + StaticDemo.getY());StaticDemo a = new StaticDemo();StaticDemo b = new StaticDemo();a.setX(1);a.setY(2);b.setX(3);b.setY(4);System.out.println("静态变量a.x=" + a.getX());System.out.println("实例变量a.y=" + a.getY());System.out.println("静态变量b.x=" + b.getX());System.out.println("实例变量b.y=" + b.getY());}
}
输入格式:
无
输出格式:
静态变量x=0 实例变量y=0 静态变量a.x=3 实例变量a.y=4 静态变量b.x=3 实例变量b.y=4
输入样例:复制
在这里给出一组输入。例如:
输出样例:复制
在这里给出相应的输出。例如:
静态变量x=0
实例变量y=0
静态变量a.x=3
实例变量a.y=4
静态变量b.x=3
实例变量b.y=4
class StaticDemo {static int x;// 1)static int y;public static int getX() {// 2}return x;}public static void setX(int newX) {x = newX;}public static int getY() {return y;}public static void setY(int newY) {y = newY;}}public class Main {public static void main(String[] args) {System.out.println("静态变量x=" + StaticDemo.getX());System.out.println("实例变量y=" + StaticDemo.getY());StaticDemo a = new StaticDemo();StaticDemo b = new StaticDemo();a.setX(1);a.setY(2);b.setX(3);b.setY(4);System.out.println("静态变量a.x=" + a.getX());System.out.println("实例变量a.y=" + a.getY());System.out.println("静态变量b.x=" + b.getX());System.out.println("实例变量b.y=" + b.getY());}
}