/**实验3.2
* 实现一个计数器类Counter,其中包含
* 域 counterValue 用来保存计数器的当前数值
* 方法increment() 计数器加1
* 方法decrement() 计数器减1
* 方法reset() 计数器清零
* */
public class adu { public static void main (String[] args) { Counter n = new Counter(); System.out.println("计数器加1: " + n.increment()); System.out.println("计数器减1: " + n.decrement()); System.out.println("计数器清零: " + n.reset()); System.out.println("计数器加1: " + n.increment()); System.out.println("计数器减1: " + n.decrement()); } } class Counter { private static int counterValue = 5; int increment() { return ++counterValue; } int decrement() { return --counterValue; } int reset() { counterValue = 0; return 0; } }
时间: 2024-10-03 10:30:35