gpt4 book ai didi

java - 如何在android中正确声明静态变量?

转载 作者:行者123 更新时间:2023-11-30 10:50:03 25 4
gpt4 key购买 nike

我进退两难,因为我不知道什么是更好的解决方案。我有一个静态变量。

我想知道声明这些变量的最佳做法是什么。假设我在 myStatic 类中有这样一个变量。

public class myStatic(){

public static int integer = 0;

/* get value */
public int getInteger() {
return integer;
}

/* set value */
public void setInteger(int nInteger) {
integer = nInteger;
}

}

现在我必须增加这个变量或减少。如何正确操作?

1)

myStatic.integer++;

2)

myStatic mystatic = new myStatic();
int integer = mystatic.getInteger();
int nInteger = integer+1;

mystatic.setInteger(iInteger);

使用解决方案 1 还是 2 更好?

最佳答案

我会选择 1,100%,也许只是因为我懒惰,但有点也是因为:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

This principle has been a key, and a huge success in my years of software engineering. A common problem among software engineers and developers today is that they tend to over complicate problems.

Principle of extreme programming (XP) that states a programmer should not add functionality until deemed necessary.

关于java - 如何在android中正确声明静态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35206389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com