★★帮我讲解一个简单的JAVA程序★★

来源:百度知道 编辑:UC知道 时间:2024/06/15 08:41:52
public class Leap {
int i = 0;
Leap increment() {Leap这句是什么意思,这是构照方法为什么要LEAP
i++;
return this;
}
void print() {
System.out.print(i);
}
public static void main(String [] args) {
Leap x = new Leap();
x.increment().increment().increment().print();//这句 为什么要怎么多increment() 又是什么意思

}
}

Leap increment() 这句的意思是:函数名为increment,函数的返回值为Leap对象

最后两句,第一句new一个Leap对象
第二句调用它的increment方法,increment方法返回Leap对象,通过返回的Leap对象再接着调用它的increment方法,一直下去

不知道有没有说明白

1.Leap increment() 是定义方法increment,返回类型是本类leap的类型
使用I++,是increment()方法希望实现的功能,没执行一次,就让i累加一次
2.多次使用increment(),就会让i累加多次。这里执行了3次,i的值就变为了3.