JAVA编写一个完整的计数器类Count,写出源代码

其中包含以下功能: 1)int类型成员变量countValue 用来保存计数器的当前数值; 2)方法increment() 返回值为countValue加1; 3)方法decrement() 返回值为countValue减1; 4)方法reset() 计数器清空成员变量countValue为0; 5)编写带参数的构造函数,用于初始化成员变量countValue; 6)在主方法中,创建对象并调用方法increment()和reset() 7)写出调用后的运行结果。

public class Count{ int countValue; Count(){ countValue=0; } public void increment() { countValue++; } public void decrement() { countValue--; } public void reset() { countValue=0; } public int getCountValue(){ return countValue; } public static void main(String args[]){ Count c = new Count(); c.increment(); System.out.println(c.getCountValue()); c.reset(); System.out.println(c.getCountValue()); } } 运行结果: 1 0

采纳哦
温馨提示:答案为网友推荐,仅供参考