java中counter怎么用,用 JAVA设计一个计数器类Counter,根据下面的提示,完成该类的声明和定义,并编写测试程序进行测试...

满意答案

00e27ab806e4881f8254fe7ae8741834.png

backy695

2015.04.28

00e27ab806e4881f8254fe7ae8741834.png

采纳率:44%    等级:9

已帮助:467人

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748/** *  * @author Administrator * */public class Counter {\x05\x05//单例模式\x05private static Counter uniqueCounter = null;\x05 \x05private Counter() {\x05  // Exists only to defeat instantiation.\x05}\x05//获取实例\x05public synchronized static Counter getInstance() {\x05   if (uniqueCounter == null) {\x05\x05   uniqueCounter = new Counter();\x05   }\x05   return uniqueCounter;\x05}\x05//计数\x05private static int count = 0;\x05/**\x05 * 获取计数 \x05 * @return\x05 */\x05public static int getCount() {\x05\x05return count;\x05}\x05/**\x05 * 互斥访问\x05 * 增加偏移量\x05 * @param offset\x05 */\x05public synchronized static void add (int offset) {\x05\x05count = count + offset;\x05}\x05/**\x05 * 互斥访问\x05 * 减少偏移量\x05 * @param offset\x05 */\x05public synchronized static void sub (int offset) {\x05\x05count = count - offset;\x05} }访问的时候用Counter.getInstance()获取实例.

04分享举报

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值