Enum实现单例模式

时间:2022-03-01 18:34:07
package com.wjy.effective;

public enum Singleton {
INSTANCE; private int numa;
private int numb;
public int add(int a,int b){
numa=a;
numb=b;
return numa+numb;
}
}

客户端代码:

package com.wjy.effective;

public class TestSingleton {
public static void main(String args[]){
Singleton instance=Singleton.INSTANCE;
int sum=instance.add(, ); System.out.println("result is: "+(sum+));
}
}

运行结果:

result is: