枚举

2020-11-10  本文已影响0人  曾梦想仗剑天涯

定义枚举类

//定义一个枚举
package demo;
enum Color {    //枚举类
  RED, GREEN, BLUE;   //实例化对象 
}
public class JavaDemo {
  public static void main(String [] args) {
    Color c = Color.RED;
    System.out.println(c);
  }
}
//获取所有的额枚举对象
package demo;
enum Color {    //枚举类
  RED, GREEN, BLUE;   //实例化对象 
}
public class JavaDemo {
  public static void main(String [] args) {
    for(Color c : Color.values()) {
      System.out.println(c);
    }
  }
}
//观察枚举与switch的处理
package demo;
enum Color {    //枚举类
  RED, GREEN, BLUE;   //实例化对象 
}
public class JavaDemo {
  public static void main(String [] args) {
    Color c = Color.RED;
    switch (c) {    //直接支持枚举
      case RED:
        System.out.println("红色");
        break;
      case GREEN:
        System.out.println("红色");
        break;
      case BLUE:
        System.out.println("红色");
        break;
    }
  }
}

Enum类

public abstract class Enum <E extends Enum<E>> 
extends Object
implements Comparable<E>,Serializable
No. 方法名称 类型 描述
01 protected Enum(String name, int ordinal) 构造 传入名字和序号
02 public final String name() 普通 获得对象名字
03 public final int ordinal() 普通 获取对象序号
//观察Enum类的存在
package demo;
enum Color {    //枚举类
  RED, GREEN, BLUE;   //实例化对象 
}
public class JavaDemo {
  public static void main(String [] args) {
    for(Color c : Color.values()) {
      System.out.println(c.ordinal() + " - " + c.name());
    }
  }
}

定义枚举结构

//在枚举类中定义其他的结构
package demo;
enum Color {    //枚举类
  RED("红色"), GREEN("绿色"), BLUE("蓝色");   //枚举对象要写在首行
  private String title;
  private Color(String title) {
    this.title = title;
  }
  public String toString() {
    return this.title;
  }
}
public class JavaDemo {
  public static void main(String [] args) {
    for(Color c : Color.values()) {
      System.out.println(c.ordinal() + " - " + c.name() + " - " + c);
    }
  }
}
//让枚举实现接口
package demo;
interface IMessage {
  public abstract String getMessage();
}
enum Color implements IMessage{    //枚举类
  RED("红色"), GREEN("绿色"), BLUE("蓝色");   //枚举对象要写在首行
  private String title;
  private Color(String title) {
    this.title = title;
  }
  public String getMessage() {
    return this.title;
  }
  public String toString() {
    return this.title;
  }
}
public class JavaDemo {
  public static void main(String [] args) {
    IMessage msg = Color.RED;
    System.out.println(msg.getMessage());
  }
}
//观察枚举中定义抽象方法
package demo;
enum Color {    //枚举类
  RED("红色") {
    public String getMessage() {
      return this.toString();
    }
  }, GREEN("绿色") {
    public String getMessage() {
      return this.toString();
    }
  }, BLUE("蓝色") {
    public String getMessage() {
      return this.toString();
    }
  };   //枚举对象要写在首行
  private String title;
  private Color(String title) {
    this.title = title;
  }
  public abstract String getMessage();
  public String toString() {
    return this.title;
  }
}
public class JavaDemo {
  public static void main(String [] args) {
    System.out.println(Color.RED.getMessage());
  }
}

枚举应用案例

    定义一个Person类,里面一定有性别,性别肯定不希望用户随意输入,所以用枚举最合适;

//使用枚举
package demo;
enum Sex {
  MALE("男"), FEMALE("女");
  private String title;
  private Sex(String title) {
    this.title = title;
  }
  public String toString() {
    return this.title;
  } 
}
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 JavaDemo {
  public static void main(String [] args) {
    System.out.println(new Person("张三", 20, Sex.MALE));
  }
}
上一篇 下一篇

猜你喜欢

热点阅读