本文共 2158 字,大约阅读时间需要 7 分钟。
public enum EnumNum01 { SUCCESS, FAILED}
新建一个类测试
public class TestEnum { public static void main(String[] args) { System.out.println(EnumNum01.SUCCESS); System.out.println(EnumNum01.FAILED); }}
输出结果:
SUCCESSFAILED
public enum EnumNum02 { A(1),B(2),C(3),D(4),E(5); private int num; EnumNum02(int num){ this.num=num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; }}
新建一个类测试:
public class EnumTest02 { public static void main(String args[]){ EnumNum02 b=EnumNum02.B; System.out.println(b.getNum()); }}
运行结果:
2
public enum EnumNum01 { A(1,"1801"),B(2,"1802"),C(3,"1803"),D(4,"1804"); private int num; private String msg; EnumNum01(int num,String msg){ this.num=num; this.msg=msg; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}
新建一个测试类:
public class EnumTest01 { public static void main(String args[]){ EnumNum01 a=EnumNum01.A; System.out.println(a); System.out.println(a.getNum()); System.out.println(a.getMsg()); }}
运行结果:
A11801
public class Singleton01 { private static Singleton01 ourInstance = new Singleton01(); public static Singleton01 getInstance() { return ourInstance; } private Singleton01() { }}
(1)、新增加一个方法
public class Singleton01 { private static Singleton01 ourInstance = new Singleton01(); public static Singleton01 getInstance() { return ourInstance; } private Singleton01() { System.out.println("这是无参构造方法......"); } public void print(){ System.out.println("这是单例中增加的方法......"); }}
(2)、新建一个类测试
public class SingletonTest { public static void main(String args[]){ Singleton01 singleton01=Singleton01.getInstance(); singleton01.print(); }}
运行结果:
这是无参构造方法......这是单例中增加的方法......
转载于:https://blog.51cto.com/doublelinux/2070249