(2) 试编写Java代码实现一个计数器类Counter,其中包含:

来源:百度知道 编辑:UC知道 时间:2024/07/02 16:51:33
(2) 试编写Java代码实现一个计数器类Counter,其中包含:
 域counterValue 用来保存计数器的当前数值;
 方法increment() 计数器加一;
 方法decrement() 计数器减一;
 方法reset() 计数器清零。
对于这个类,你计划定义几个构造函数?

public class Counter{
private int counterValue=0;

public void setCounterValue(String counterValue){
this.counterValue=counterValue;
}

public int getCounterValue(){
return this.counterValue;
}

public void increment(){
getCounterValue()+1;
}

public void decrement(){
getCounterValue()-1;
}

public void reset(){
this.setCounterValue(0);
}
}

这段代码我没有编译,你自己试一下看行不行
这里没有写main()方法,至于你说的定义几个构造函数的话,要看你想要做什么,你可以在写构造方法的时候给这个变量赋值

只供你参考用