枚举类可用于定义常量
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

ch02
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枚举类的更多相关文章

  1. Enum枚举类|注解Annotation

    Enum枚举类 ①枚举类和普通类的差别: 使用 enum 定义的枚举类默认继承了 java.lang.Enum 类 枚举类的构造器仅仅能使用 private 訪问控制符 枚举类的全部实例必须在枚举类中 ...

  2. Java中的enum枚举类

    首先说说为什么要写这个enum枚举类吧,是群里有个新手问:怎样把enum类中的值遍历得到,其实自己用的也很少.自己也是确实不知道,于是我去网上搜了不少,总结了些,希望对大家有帮助:首先我说说怎样遍历枚 ...

  3. Enum 枚举类

    目录 Enum 枚举类 基础 定义与用途 基本方法 示例 进阶 实现原理 枚举与Class对象 自定义枚举类和构造方法及toString() Enum中使用抽象方法来实现枚举实例的多态性 Enum与接 ...

  4. Java 基础 enum枚举类 的创建/使用/接口继承 ,以及手动创建枚举类的对象为:public static final

    笔记: import java.lang.*; /**一:枚举类 : enum Season implements info { s1(),s2(),s3(),s4() }; //s1--s4 放在S ...

  5. Java Enum 枚举类的values方法

    Enum类和enum关键字定义的类型都有values方法,但是点进去会发现找不到这个方法.这是因为java编译器在编译这个类(enum关键字定义的类默认继承java.lang.Enum)的时候 自动插 ...

  6. Enum枚举类使用集合

    1.使用扩展方法使用枚举值对于的Description属性值 public static class EnumExtenstion { public static string GetDescript ...

  7. java enum 枚举类

    图一代码: public enum LogMethodEnum { WEBCSCARDVALID("返回值"), WEBCSVERIFYPASSWORD("返回值&quo ...

  8. java 枚举 类 enum

    public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...

  9. Java分享笔记:自定义枚举类 & 使用enum关键字定义枚举类

    在JDK1.5之前没有enum关键字,如果想使用枚举类,程序员需要根据Java语言的规则自行设计.从JDK1.5开始,Java语言添加了enum关键字,可以通过该关键字方便地定义枚举类.这种枚举类有自 ...

随机推荐

  1. TS和C#的差异

    1. TS中let a : () => void; 假设有个class  B,  B里有个方法 c; 不能像C#委托一样, a = B.c;...........如果这样的话方法c里调用的thi ...

  2. mysqli链接数据库

    <?php $uid = $_GET['uid']; $host = 'localhost';$database = 'test';$username = 'root';$password = ...

  3. Pycharm--flake8的配置使用

    前言:Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具.Flake8检查规则灵活,支持集成额外插件,扩展性强. 一.安装flake8 进入虚拟环境: pip inst ...

  4. AVL树实现记录

    https://github.com/xieqing/avl-tree An AVL Tree Implementation In C There are several choices when i ...

  5. 跳转Activity时清除当前Activity

    void GotoMainActivity(){ Intent intent = new Intent(ProductionInformationActivity.this, MainActivity ...

  6. 这是一个新的开始at this very monment

    简单的介绍下此时自己的情况,和对近几年所做的事做一个总结,并展望下自己的未来和对自己的期待.我会将我的博客分成两种,一个是我的学习笔记,另一个是我的生活记录. 2018年六月底我毕业于一个普通的二本, ...

  7. c#: .net framework 2.0支持扩展方法的办法

    c#之扩展方法是个好方法,可惜只在.net framework 3.5及以上版本中用. 2.0版本若用,其编译报错如下: 错误 无法定义新的扩展方法,因为找不到编译器所需的类型“System.Runt ...

  8. 使用python画一只佩奇

    打开界面: 打开python shell界面. 建立脚本: 单击"file"——"new file"来建立脚本. 编写代码: 具体的代码如下. import t ...

  9. 关于键盘事件对象code值

    e.keyCode || e.which || e.charCode; //IE只有keyCode属性,FireFox中有which和charCode属性,Opera中有keyCode和which属性 ...

  10. netty服务器端启动

    package com.imooc.netty.ch3; import com.imooc.netty.ch6.AuthHandler; import io.netty.bootstrap.Serve ...