java入门篇 (32)枚举类

一、概念

如果一个类拥有有限个固定的对象,那么这个类就是枚举类。

二、定义一个简单的枚举类

2.1 自定义枚举类

定义枚举类:

public class Week {
    String str;
    public static final Week Sunday = new Week("Sun");
    public static final Week Monday = new Week("Mon");
    public static final Week Tudesday = new Week("Tue");
    public static final Week Wednesday = new Week("Wed");
    public static final Week Thursday = new Week("Thu");
    public static final Week Friday = new Week("Fri");
    public static final Week Saturday = new Week("Sat");
    private Week(String str){
        this.str = str;
    }
    @Override
    public String toString() {
        return this.str;
    }
}

测试类:


public class text1 {
    public static void main(String[] args) {
        Week sunday = Week.Sunday;
        Week monday = Week.Monday;
        System.out.println(sunday + " " + monday);
    }
}

2.2 使用enum关键字定义简单枚举类

如果只在里面加入成员,输出时将会把成员名字输出。

public enum myenum {
    Sat,Sun,Mon;
}

public class Text {
    public static void main(String[] args) {
        myenum sat = myenum.Sat;
        myenum sun = myenum.Sun;
        System.out.println(sat);
        System.out.println(sun);
    }
}

结果:

Sat
Sun

2.3 使用enum关键字定义稍微复杂的枚举类

定义枚举类:

enum myenum {
    people1("张三","10"),
    people2("李四","19"),
    people3("王五","10");
    final String myname;
    final String myage;
    private myenum(String myname, String myage) {
        this.myname = myname;
        this.myage = myage;
    }

    @Override
    public String toString() {
        System.out.println(this.myname + " " + this.myage);
        return "输出信息:";
    }
}

测试类:

public class Text {
    public static void main(String[] args) {
        myenum people1 = myenum.people1;
        myenum people2 = myenum.people2;
        myenum people3 = myenum.people3;
        System.out.println(people1);
        System.out.println(people2);
        System.out.println(people3);
    }
}

结果:

张三 10
输出信息:
李四 19
输出信息:
王五 10
输出信息:

注意:

  • 枚举类的成员变量之间用逗号隔开!!!!!
  • switch…case语句允许用枚举类型作为参数
public class Text {
    public static void main(String[] args) {
        myenum people1 = myenum.people1;
        switch (people1) {
            case people1:
                System.out.println("猪");
                break;
            case people2:
                System.out.println("牛");
                break;
        }
    }
}

结果:
猪

2.4 枚举类常用的方法

  • int ordinal ()
    返回枚举项的序号
public class Text {
    public static void main(String[] args) {
        myenum people1 = myenum.people1;
        myenum people2 = myenum.people2;
        System.out.println(people2.ordinal());
    }
}
  • int compareTo (E o)
    比较两个枚举项的 返回的是两个枚举项序号的 差值
public class Text {
    public static void main(String[] args) {
        myenum people1 = myenum.people1;
        myenum people2 = myenum.people2;
        System.out.println(people1.compareTo(people2));
    }
}
  • String name ()
    获取枚举项的名称
public class Text {
    public static void main(String[] args) {
        myenum people1 = myenum.people1;
        String name = people1.name();
    }
}
  • String toString ()
    获取枚举项的名称
public class Text {
    public static void main(String[] args) {
        myenum people1 = myenum.people1;
        String name = people1.toString();
    }
}

  • T valueOf (Class < T > type, String name)
    用来获取指定的枚举项 参数1:枚举类对应的字节码对象 参数2 枚举项的名称
public class Text {
    public static void main(String[] args) {
        myenum myenum = net.myenum.valueOf(myenum.class, "people1");
        myenum.show();
    }
}

结果:
张三 10
  • values()
    获取所有的枚举项
public class Text {
    public static void main(String[] args) {
        myenum[] values = myenum.values();
        for (myenum myenum : values) {
            myenum.show();
        }
    }
}

结果:
张三 10
李四 19
王五 10

先定义一个枚举类:

enum myenum {
    people1("张三","10"),
    people2("李四","19"),
    people3("王五","10");
    final String myname;
    final String myage;
    private myenum(String myname, String myage) {
        this.myname = myname;
        this.myage = myage;
    }
    public void show() {
        System.out.println(this.myname + " " + this.myage);
    }
}

三、JDK7的一些新特性

3.1 用二进制表示整数

public class Text {
    public static void main(String[] args) {
       int a = 0b101;
        System.out.println(a);
    }
}

结果:
5

3.2 数字字面量可以出现下划线

public class Text {
    public static void main(String[] args) {
        int a = 100_199;
        System.out.println(a);
    }
}

结果:
100199

注意:

  • 不能出现在进制标识和数值之间
  • 不能出现在数值开头和结尾
  • 不能出现在小数点旁边

3.3泛型简化

参考:
http://blog.sina.com.cn/s/blog_6dc41baf010144mu.html

3.4 异常的多个catch合并

参考:
http://www.importnew.com/7015.html

3.5 try-with-resources 语句

参考:
https://www.cnblogs.com/hihtml5/p/6505317.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值