调用同一个类中的方法,
1.对于非静态方法,需要创建对象才可以调用
public static void main(String[] args) {
new Beishu().problem1();
}
public void problem1() {
int sum = 0;
for (int i = 1; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum = sum + i;
}
}
System.out.println("sum=" + sum);
}
2.对于静态方法,就可以直接调用
public static void main(String[] args) {
problem2();
}
public static void problem2() {
Integer front = 1;
Integer after = 1;
Integer tem;
//Integer i=1;
Integer sum = 0;
while (after < 4000000) {
tem = front + after;
front = after;
after = tem;
System.out.println(front);
if (after % 2 == 0 && after < 4000000) {
sum = after + sum;
}
}
System.out.println("sum=" + sum);
}
注意:静态方法、变量都是属于类的,当你调用类时那些静态方法和属性就已经存在了,也就是说直接调用类名.属性/方法名就可以,而非静态属性和方法必须得实例化对象才能调用,你可以这么理解,当程序跑到静态方法里时,非静态方法这时还是未存在的,不过静态调用非静态也是可以的,不过不能直接调用罢了,你得先实例化对象后再调用才行