Nom*_*sta 7 java android kotlin
实际上的问题是,哪种方法更好,您使用哪种约定?
例如,为了提高可读性,我们在 Java 类中添加了这个字段:
class Game {
private static final int MAX_PLAYERS = 10;
public boolean canWePlay(int playersCount) {
return playersCount <= MAX_PLAYERS;
}
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
现在更好的方法是使用伴随对象:
class Game {
fun canWePlay(playersCount: Int): Boolean {
return playersCount < MAX_PLAYERS
}
companion object {
val MAX_PLAYERS = 10
}
}
Run Code Online (Sandbox Code Playgroud)
或和const val课外:
private const val MAX_PLAYERS = 10
class Game {
fun canWePlay(playersCount: Int): Boolean {
return playersCount < MAX_PLAYERS
}
}
Run Code Online (Sandbox Code Playgroud)
或者你使用不同的方法?
另一个问题是,如果我们使用第二个约定,我们应该使用驼峰式大小写还是带下划线的大写?(我正在谈论 Android 代码)。
| 归档时间: |
|
| 查看次数: |
1418 次 |
| 最近记录: |