不确定单例
Not sure about Singleton
如果我有一个单身人士 class 比如:
public class MySingleton(){
private static MySingleton istance;
private int element;
private MySingleton(){element = 10;}
public static MySingleton getIstance() {
if(istance == null)
istance = new Mysingleton();
return istance;
}
public void setElement(int i ){
element = i;
}
public int getElement(){
return element;
}
}
我想通过调用
来更改元素的值
MySingleton.getIstance().setElement(20)
它会改变距离的元素值吗?这是一个例子:
... main () {
MySingleton.getIstance().setElement(20);
System.out.prinln(MySingleton.getIstance().getElement());
// It displays 10, why ?
我不确定你的代码是否真的有效,azurefrog 怎么说,让你的代码同步,在你的行 public static getIstance() {
中你需要设置 return 类型。
我建议您使用 enum
,因为它更简单且线程安全(但不是您的 getter/setter)
public enum MySingleton() {
INSTANCE;
private int element = 10;
public void setElement(int element) { this.element = element; }
public int getElement() { return element; }
}
MySingleton.INSTANCE.setElement(20);
System.out.prinln(MySingleton.INSTANCE.getElement()); // prints 20.
我不确定你上面的代码块是被复制进来还是只是重新输入,但我看到了一些基本的编译问题——当你在 getInstance 中设置 MySingleton 时,你需要检查大小写。此外,您的 class 声明不应包含(括号)。修好这两个东西和运行基本的main后,我得到了20.
这与您所拥有的相同 - 没有同步或其他任何东西,但在单个线程上似乎没有必要。
public class MySingleton{
private static MySingleton istance;
private int element;
private MySingleton(){element = 10;}
public static MySingleton getIstance() {
if(istance == null)
istance = new MySingleton();
return istance;
}
public void setElement(int i ){
element = i;
}
public int getElement(){
return element;
}
public static void main(String[] args) {
System.out.println(MySingleton.getIstance().getElement());
MySingleton.getIstance().setElement(20);
System.out.println(MySingleton.getIstance().getElement());
}
}
应该有
的输出
10
20
如果我有一个单身人士 class 比如:
public class MySingleton(){
private static MySingleton istance;
private int element;
private MySingleton(){element = 10;}
public static MySingleton getIstance() {
if(istance == null)
istance = new Mysingleton();
return istance;
}
public void setElement(int i ){
element = i;
}
public int getElement(){
return element;
}
}
我想通过调用
来更改元素的值MySingleton.getIstance().setElement(20)
它会改变距离的元素值吗?这是一个例子:
... main () {
MySingleton.getIstance().setElement(20);
System.out.prinln(MySingleton.getIstance().getElement());
// It displays 10, why ?
我不确定你的代码是否真的有效,azurefrog 怎么说,让你的代码同步,在你的行 public static getIstance() {
中你需要设置 return 类型。
我建议您使用 enum
,因为它更简单且线程安全(但不是您的 getter/setter)
public enum MySingleton() {
INSTANCE;
private int element = 10;
public void setElement(int element) { this.element = element; }
public int getElement() { return element; }
}
MySingleton.INSTANCE.setElement(20);
System.out.prinln(MySingleton.INSTANCE.getElement()); // prints 20.
我不确定你上面的代码块是被复制进来还是只是重新输入,但我看到了一些基本的编译问题——当你在 getInstance 中设置 MySingleton 时,你需要检查大小写。此外,您的 class 声明不应包含(括号)。修好这两个东西和运行基本的main后,我得到了20.
这与您所拥有的相同 - 没有同步或其他任何东西,但在单个线程上似乎没有必要。
public class MySingleton{
private static MySingleton istance;
private int element;
private MySingleton(){element = 10;}
public static MySingleton getIstance() {
if(istance == null)
istance = new MySingleton();
return istance;
}
public void setElement(int i ){
element = i;
}
public int getElement(){
return element;
}
public static void main(String[] args) {
System.out.println(MySingleton.getIstance().getElement());
MySingleton.getIstance().setElement(20);
System.out.println(MySingleton.getIstance().getElement());
}
}
应该有
的输出10
20