enum枚举类
枚举类可用于定义常量
ch01
package edu.nf.demo.ch01; /**
*
* 枚举类型
*/
public enum Color { /**
* 红色
*/
RED,
/**
* 黄色
*/
YELLOW,
/**
* 蓝色
*/
BLUE,
/**
* 绿色
*/
GREEN,
/**
* 黑色
*/
BLACK }
Color
ch01
public class Main {
public static void main(String[] args) {
//test1();
test2(Color.RED);
//test3(Color.RED);
}
private static void test1(){
//Color.RED得到的是一个Color枚举元素的实例
System.out.println("枚举实例:"+Color.RED);
System.out.println("枚举元素名称:"+Color.RED.name());
System.out.println("枚举元素下标:"+Color.BLUE.ordinal());
System.out.println(Color.BLUE);
System.out.println(Color.BLUE.name());
System.out.println(Color.BLUE.ordinal());
System.out.println("---------------");
for (Color value : Color.values()) {
System.out.println(value);
}
}
private static void test2(Color color){
System.out.println(color);
switch (color) {
case RED:
System.out.println("This is red color");
break;
case BLUE:
System.out.println("This is blue color");
break;
default:
System.out.println("no color");
}
}
private static void test3(Color color){
System.out.println(color);
if(color.equals(Color.RED)){
System.out.println("This is red color");
}else if(color.equals(Color.BLUE)){
System.out.println("This is blue color");
}else{
System.out.println("no color");
}
}
}
main
public enum Color {
/**
* 红色
*/
RED(0),
/**
* 黄色
*/
YELLOW(1),
/**
* 蓝色
*/
BLUE(2),
/**
* 绿色
*/
GREEN(3),
/**
* 黑色
*/
BLACK(4)
; //分隔符
//声明一个实例变量
private Integer color;
//构造方法
Color(Integer color){
this.color = color;
}
//提供一个get方法用于获取color变量的值
public Integer getColor() {
return color;
}
}
Color(枚举自定义构造函数以及添加方法)
ch02
public class Main {
public static void main(String[] args) {
System.out.println(Color.YELLOW.name());
System.out.println(Color.YELLOW.getColor());
}
}
main
ch03
public enum Color {
/**
* 红色
*/
RED{
@Override
public void printColor() {
System.out.println("This is red.");
}
},
/**
* 蓝色
*/
BLUE{
@Override
public void printColor() {
System.out.println("This is blue");
}
};
/**
* 在枚举中声明一个抽象方法,
* 并且在每一个枚举元素中是使用匿名内部类的方式进行实现
*/
public abstract void printColor();
}
Color枚举中定义抽象方法,类似于继承重载里面的方法
ch03
public class Main {
public static void main(String[] args) {
Color.RED.printColor();
}
}
main
ch04
public enum Color implements PrintInf {
/**
* 红色
*/
RED{
@Override
public void printColor() {
System.out.println("This is red.");
}
},
/**
* 蓝色
*/
BLUE{
@Override
public void printColor() {
System.out.println("This is blue.");
}
}
}
public enum Color2 implements PrintInf {
/**
* 红色
*/
RED("red"),
/**
* 蓝色
*/
BLUE("blue")
;
private String color;
Color2(String color){
this.color = color;
}
@Override
public void printColor() {
System.out.println("Print color: " + color);
}
}
Color继承接口
ch04
public interface PrintInf {
void printColor();
}
public class Main {
public static void main(String[] args) {
PrintInf pf = Color.RED;
pf.printColor();
Color2.RED.printColor();
}
}
接口和main
ch05
public interface Food {
/**
* 开胃菜
*/
enum Appetizer implements Food {
/**
* 沙拉
*/
SALAD("沙拉"),
/**
* 汤
*/
SOUP("汤")
;
private String foodName;
Appetizer(String foodName){
this.foodName = foodName;
}
public String getFoodName() {
return foodName;
}
}
/**
* 主食
*/
enum MainCourse implements Food {
/**
* 千层面
*/
LASAGNE("千层面"),
/**
* 卷饼
*/
BURRITO("卷饼")
;
private String foodName;
MainCourse(String foodName){
this.foodName = foodName;
}
public String getFoodName() {
return foodName;
}
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Food.Appetizer.SALAD.getFoodName());
}
}
Color在接口中声明枚举类型
enum枚举类的更多相关文章
- Enum枚举类|注解Annotation
Enum枚举类 ①枚举类和普通类的差别: 使用 enum 定义的枚举类默认继承了 java.lang.Enum 类 枚举类的构造器仅仅能使用 private 訪问控制符 枚举类的全部实例必须在枚举类中 ...
- Java中的enum枚举类
首先说说为什么要写这个enum枚举类吧,是群里有个新手问:怎样把enum类中的值遍历得到,其实自己用的也很少.自己也是确实不知道,于是我去网上搜了不少,总结了些,希望对大家有帮助:首先我说说怎样遍历枚 ...
- Enum 枚举类
目录 Enum 枚举类 基础 定义与用途 基本方法 示例 进阶 实现原理 枚举与Class对象 自定义枚举类和构造方法及toString() Enum中使用抽象方法来实现枚举实例的多态性 Enum与接 ...
- Java 基础 enum枚举类 的创建/使用/接口继承 ,以及手动创建枚举类的对象为:public static final
笔记: import java.lang.*; /**一:枚举类 : enum Season implements info { s1(),s2(),s3(),s4() }; //s1--s4 放在S ...
- Java Enum 枚举类的values方法
Enum类和enum关键字定义的类型都有values方法,但是点进去会发现找不到这个方法.这是因为java编译器在编译这个类(enum关键字定义的类默认继承java.lang.Enum)的时候 自动插 ...
- Enum枚举类使用集合
1.使用扩展方法使用枚举值对于的Description属性值 public static class EnumExtenstion { public static string GetDescript ...
- java enum 枚举类
图一代码: public enum LogMethodEnum { WEBCSCARDVALID("返回值"), WEBCSVERIFYPASSWORD("返回值&quo ...
- java 枚举 类 enum
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...
- Java分享笔记:自定义枚举类 & 使用enum关键字定义枚举类
在JDK1.5之前没有enum关键字,如果想使用枚举类,程序员需要根据Java语言的规则自行设计.从JDK1.5开始,Java语言添加了enum关键字,可以通过该关键字方便地定义枚举类.这种枚举类有自 ...
随机推荐
- MyEclipse2014破解版安装教程
下载安装包和破解程序脚本 1.下载地址 链接:https://pan.baidu.com/s/1XuMweEz602zcoGqwPb2xTA 提取码:idsx 两个文件:myeclipse-pro- ...
- jquery cdn bootstrap静态资源库问题
使用微软静态资源库 <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js">&l ...
- 《Go程序设计语言》读书笔记-函数
函数包含连续执行的语句,可以使用代码中通过调用函数来执行他们,函数能够将一个复杂的工作切分成多个更小的模块,使多人写作变得容易.另外,函数对他的使用者隐藏了实现细节.这几方面的特性使得函数成为多数编程 ...
- 自己实现ArrayList
思路: 一 载体 ArrayList是一个集合容器,必然要有一个保存数据的载体. public class MyArraylist { private final static int INIT_CO ...
- 在Html页面中调用ajax代码
以最原始的XMLHttpRequest形式,实现ajax. 封装的方法 1 /** 2 * 发送一个 AJAX 请求 3 * @param {String} method 请求方法 4 * @para ...
- C++中为什么有时要使用extern "C"
extern "C"的作用 在C++引用lua的头文件时,我们总会写成: extern "C" { #include "lua.h" #in ...
- 递归可视化之汉诺塔的动画实现(turtle海龟)
import turtle class Stack: def __init__(self): self.items = [] def isEmpty(self): def push(self, ite ...
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-1虚拟机安装及环境初始化
1.软件准备: VMware-workstation-full-14.1.2-8497320.exe CentOS-7-x86_64-DVD-1804.iso 2.VMare激活码: AU5WA-0E ...
- css与dom的渲染与解析
js阻塞文档渲染与解析那么css呢? 结论一.css:阻塞渲染,不阻塞dom解析 <head> <script> document.addEventListener('DOMC ...
- MAVEN工程相关配置
MAVEN工程插件安装: Name: MavenArchiver Location: https://repo1.maven.org/maven2/.m2e/connectors/m2eclipse- ...