枚举ordinal与name

枚举ordinal与name区别

定义一个枚举

public enum HttpMethod {
    GET, POST, PUT, DELETE
}

获取ordinal

        System.out.println(HttpMethod.GET.ordinal());
        System.out.println(HttpMethod.POST.ordinal());
        System.out.println(HttpMethod.PUT.ordinal());
        System.out.println(HttpMethod.DELETE.ordinal());

结果
0
1
2
3

获取到的是下标,从0开始

获取name

        System.out.println(HttpMethod.GET.name());
        System.out.println(HttpMethod.POST.name());
        System.out.println(HttpMethod.PUT.name());
        System.out.println(HttpMethod.DELETE.name());

结果
GET
POST
PUT
DELETE

获取到默认的值

你可能感兴趣的:(JAVA)