zoukankan      html  css  js  c++  java
  • java基础>枚举 小强斋

    1、枚举的概念

    多例设计:一个类只能产生有限多个实例化对象,那么枚举的功能实际上就属于这种功能的实现,JDK 1.5之后增加了新的关键字:enum。

    范例:定义一个枚举类

    enum Color {
    
    	RED, GREEN, BLUE;
    
    }
    
    public class EnumDemo01 {
    
    	public static void main(String args[]) {
    
    		Color c = Color.RED;
    
    		System.out.println(c);
    
    	}
    
    };

    2、enum关键字和Enum类

    enum Color {
    
    	RED, GREEN, BLUE;
    
    }
    
    public class EnumDemo02 {
    
    	public static void main(String args[]) {
    
    		for (Color c : Color.values()) {
    
    			System.out.println(c.ordinal() + " --> " + c.name());
    
    		}
    
    	}
    
    };
    
    枚举通过enum关键字定义,定义的枚举就相当于一个类继承了Enum类。

    3、定义属性及方法

    使用enum定义了一个枚举之后也可以像普通类那样定义属性和方法,包括构造方法,但是一定要注意的是,所有的构造方法一定是private。否则报错 Illegal modifier for the enum constructor; only private is permitted.
    enum Color {
    
    	RED("红色"), GREEN("绿色"), BLUE("蓝色");
    
    	private Color(String title) {
    
    		this.setTitle(title);
    
    	}
    
    	private String title;
    
    	public void setTitle(String title) {
    
    		this.title = title;
    
    	}
    
    	public String getTitle() {
    
    		return this.title;
    
    	}
    
    }
    
    public class EnumDemo03 {
    
    	public static void main(String args[]) {
    
    		for (Color c : Color.values()) {
    
    			System.out.println(c.ordinal() + " --> " + c.name() + " --> "
    					+ c.getTitle());
    
    		}
    
    	}
    
    };
    
    在一般的开发中完全可以通过类的设计来达到枚举的功能。

    4、实现接口   

    枚举本身也可以实现接口,但是需要注意的是,一旦一个枚举实现了接口之后,枚举中的每个对象都必须分别的实例化这些接口中提供的抽像方法。
    interface Info {
    
    	public String getColorInfo();
    
    }
    
    enum Color implements Info {
    
    	RED("红色") {
    
    		public String getColorInfo() {
    
    			return this.getTitle();
    
    		}
    
    	},
    	GREEN("绿色") {
    
    		public String getColorInfo() {
    
    			return this.getTitle();
    
    		}
    
    	},
    	BLUE("蓝色") {
    
    		public String getColorInfo() {
    
    			return this.getTitle();
    
    		}
    
    	};
    
    	private Color(String title) {
    
    		this.setTitle(title);
    
    	}
    
    	private String title;
    
    	public void setTitle(String title) {
    
    		this.title = title;
    
    	}
    
    	public String getTitle() {
    
    		return this.title;
    
    	}
    
    }
    
    public class EnumDemo04 {
    
    	public static void main(String args[]) {
    
    		for (Color c : Color.values()) {
    
    			System.out.println(c.ordinal() + " --> " + c.name() + " --> "
    					+ c.getColorInfo());
    
    		}
    
    	}
    
    };
    

    5、定义抽象方法

    在枚举中还可以定义抽象方法,但是与实现接口一样的是,每一个枚举对象都要分别实现此方法。
    enum Color implements Info {
    
    	RED("红色") {
    
    		public String getColorInfo() {
    
    			return this.getTitle();
    
    		}
    
    	},
    	GREEN("绿色") {
    
    		public String getColorInfo() {
    
    			return this.getTitle();
    
    		}
    
    	},
    	BLUE("蓝色") {
    
    		public String getColorInfo() {
    
    			return this.getTitle();
    
    		}
    
    	};
    
    	private Color(String title) {
    
    		this.setTitle(title);
    
    	}
    
    	private String title;
    
    	public void setTitle(String title) {
    
    		this.title = title;
    
    	}
    
    	public String getTitle() {
    
    		return this.title;
    
    	}
    
    	public abstract String getColorInfo();
    
    }
    
    public class EnumDemo05 {
    
    	public static void main(String args[]) {
    
    		for (Color c : Color.values()) {
    
    			System.out.println(c.ordinal() + " --> " + c.name() + " --> "
    					+ c.getColorInfo());
    
    		}
    
    	}
    
    };
    

    6、枚举的应用

    枚举的最大特点实际上是限制了一个类的取值范围,例如:在设置习惯年别的时候,只能是男或女,那么此时应用枚举是最合适的地方。
    enum Sex {
    
    	MALE("男"), FEMALE("女");
    
    	private String name;
    
    	private Sex(String name) {
    
    		this.name = name;
    
    	}
    
    	public String toString() {
    
    		return this.name;
    
    	}
    
    }
    
    class Person {
    
    	private String name;
    
    	private int age;
    
    	private Sex sex;
    
    	public Person(String name, int age, Sex sex) {
    
    		this.name = name;
    
    		this.age = age;
    
    		this.sex = sex;
    
    	}
    
    	public String toString() {
    
    		return "姓名:" + this.name + ",年龄;" + this.age + ",性别:" + this.sex;
    
    	}
    
    };
    
    public class EnumDemo06 {
    
    	public static void main(String args[]) {
    
    		System.out.println(new Person("张三", 20, Sex.MALE));
    
    	}
    
    };
    
    但是通过其他的代码也可以满足此种要求,所以枚举在开发中是否使用并不是绝对的。

    7、枚举与switch语句

    switch语句,case分支条件后面只能是int,char或者枚举类型
    public enum Animal {
    
    dog,cat,pig;
    
    static Animal getValue(String  animal)
    
    {
    
            return  valueOf(animal.toLowerCase());
    
    }
    
    }
    
    switch
    public class Client {
    
            public void caseAnimal(String animal) {
    
                   switch(Animal.getValue(animal)) {
    
                   case  dog:
    
                           System.out.println("thisis a dog");
    
                           break;
    
                   case  pig:
    
                           System.out.println("thisis a pig ");
    
                           break;
    
                   case  cat:
    
                           System.out.println("thisis a cat");
    
                           break;
    
     
    
                   default:
    
                           System.out.println("idon't know what is this!");
    
                           break;
    
                   }
    
            }
    
     
    
            public static void main(String[] args) {
    
                   new Client().caseAnimal("dog");
    
     
    
            }
    
    }
    
  • 相关阅读:
    什么是API
    理解RESTful架构
    SDN的深入思考(1):SDN的核心本质到底是什么?
    SDN-数据控制分离
    浅析html+css+javascript之间的关系与作用
    python调用win32接口进行截图
    解决tensorflow问题:Your CPU supports instructions that this TensorFlow binary was not compiled to use:
    去除警告: FutureWarning: In future, it will be treated as `np.float64 == np.dtype(float).type`.
    Anaconda清华大学开源镜像
    python控制windows剪贴板,向剪贴板中写入图片
  • 原文地址:https://www.cnblogs.com/xiaoqiangzhaitai/p/5637605.html
Copyright © 2011-2022 走看看