集册 Java实例教程 定义一组固定的相关常数

定义一组固定的相关常数

欢马劈雪     最近更新时间:2020-01-02 10:19:05

609
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用枚举类型。

import java.util.Arrays;
/* 
 来自 
*时 代 J a v a 公 众 号 - nowjava.com*/


enum Direction {

  East, WEST, NORTH, SOUTH

}

public class Main {

    public static void main(String[] args) {

        Direction field = Direction.WEST;


        System.out.println("field.name(): " + field.name());

        System.out.println("field.ordinal(): " + field.ordinal());

        System.out.println("field.toString(): " + field.toString());

        System.out.println("field.isEqual(WEST): " + field.equals(Direction.WEST));

        System.out.println("field.isEqual(\"WEST\"'): " + field.equals("WEST"));


        System.out.println("field == WEST: " + (field == Direction.WEST));
/* from 时   代     Java  公  众  号 - nowjava.com*/


        System.out.println("field.compareTo(WEST): " + field.compareTo(Direction.WEST));

        System.out.println("field.compareTo(PASSWORD): " + field.compareTo(Direction.East));


        System.out.pr
展开阅读全文