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(6, 7); System.out.println("result is: "+(sum+1)); } }
运行结果:
result is: 14