什么时候该用成员变量、什么时候用方法变量?
2016-07-04 08:45
357 查看
Use class level field or method variable?(什么时候该用成员变量、什么时候用方法变量?)
If it’s strongly associated with the class, make it static.If it’s strongly associated with an instance, make it a non–static member.
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中的其他方法所共享的,那么就应该是类变量。相关文章推荐
- [Cloud Computing]Mechanisms: Attribute-Based Access Control System
- Swift3中方法可变参数语法的一些改变
- Swift3中方法可变参数语法的一些改变
- Swift3中方法可变参数语法的一些改变
- Swift3中方法可变参数语法的一些改变
- Swift3中方法可变参数语法的一些改变
- Swift3中方法可变参数语法的一些改变
- Retrofit2.0详解
- 嵌入式QT软键盘
- 7.1
- DHCP配置
- 聊聊同步、异步、阻塞与非阻塞
- IOS人脸识别
- 把普通对象转换成json格式的对象的简单实例
- Java集合源码分析(四)Vector<E>
- vuejs学习网站推荐
- 配置并检验 RIPng 路由
- 配置并检验 S1 上的安全功能
- 三消游戏的理解(2)
- 轻量级分布式 RPC 框架