Fork me on GitHub

static

static存在的时候,调用b2.setN(5),sum=6,去掉static,sum从0开始

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class B{
int n;
static int sum= 0;
void setN(int n){
this.n= n;
System.out.println("this.n= "+ this.n);
}
int getSum(){
for(int i= 1;i<= n; i++){
sum= sum+ i;
}
System.out.println(sum);
return sum;
}
}
public class Example4_9 {
public static void main(String[] args) {
B b1=new B(), b2= new B();
b1.setN(3);
b2.setN(5);
int a1= b1.getSum();
int a2= b2.getSum();
System.out.println(a1+ a2);
}
}

Your support will encourage me to continue to create!