我正在大学学习 Java 初学者类(class),我很困惑它是如何编译/执行的 以下是代码:
public class Counter {
private int counter;
private int end;
public Counter(int start, int end) {
this.counter = start;
this.end = end;
}
public String toString() {
return "[Counter counter=" + counter + " end=" + end + "]";
}
public int getCounter() {
return counter;
}
public void count(int increment) {
if (counter < end) {
counter += increment;
}
}
public void count() {
count(1);
}
}
其他类
public class CounterProgram {
private Counter counter;
public void init() {
counter = new Counter(1, 3);
}
public void run() {
System.out.println(counter);
counter.count();
System.out.println(counter);
counter.count();
System.out.println(counter);
counter.count();
System.out.println(counter);
}
public static void main(String[] args) {
CounterProgram counterProgram = new CounterProgram();
counterProgram.init();
counterProgram.run();
}
}
我不确定 count(1)
在方法 public void count
中的含义有人能解释一下吗?感谢您的宝贵时间!
最佳答案
count(1)
是对带有参数 1
(int
) 的 count
方法的调用:
public void count(int increment)
关于java - 理解简单的计数器代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21905802/