什么时候该用成员变量、什么时候用方法变量?

Use class level field or method variable?(什么时候该用成员变量、什么时候用方法变量?)

  1. If it’s strongly associated with the class, make it static.
  2. If it’s strongly associated with an instance, make it a non–static member.
  3. If it’s strongly associated with a method invocation, or the current thread, or you can’t decide about (1) or (2), make it method-local.

Note: 这是SO的一个回答,以后再思考这个问题。

UPDATE :
  • 2017-03-04: 对于Java来说,对象是抽象的,将一系列的member和行为组装起来,所以在区分class变量和方法局部变量就很容易。然后另外class的变量的作用是共享,如果是可以被其他类或本Class中的其他方法所共享的,那么就应该是类变量。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值