1
时表现出相同的价值我有这样一段代码:不同的实例变量使用GET方法
class Test {
public static void main (String[] args){
Base b1, b2;
b1= new Base(1);
b2= new Base(2);
System.out.println(b1.getX());
System.out.println(b2.getX());
}
}
public class Base {
static int x;
public Base(){
x=7;
}
public Base(int bM) {
x=bM;
}
public int getX() {
return x;
}
}
我被告知,这一计划将返回值2和2,但我不明白为什么。据我所知,它应该显示1和2.有人可以解释或给出一个解释的链接?谢谢。
线索在'static'中。这应该可以帮助你:http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html –
我应该补充一点,大多数开发人员并不认为使用这样的静态编程很好。 –