java基礎 java中枚舉的應用,java基礎枚舉應用

來源:互聯網
上載者:User

java基礎 java中枚舉的應用,java基礎枚舉應用

package com.swift.meiju;import org.junit.Test;public class Demo{     @Test     public void test() {        System.out.println(Meiju.C.getScore());     }}

上面測試,用了Junit

下邊枚舉

package com.swift.meiju;import org.junit.Test;public enum Meiju {    //考試分數A B C D E 五個等級    A("90~100"),B("80~89"),C("70~79"),D("60~69"),E("0~59");    private String score;    private Meiju(String score) {        this.score=score;    }    public String getScore() {        return this.score;    }}

 

枚舉是什麼?

相當於

package com.swift.meiju;public class Enumer {        private Enumer() {}    public static final Enumer A=new Enumer();    public static final Enumer B=new Enumer();    public static final Enumer C=new Enumer();    public static final Enumer D=new Enumer();    public static final Enumer E=new Enumer();}

有點像單例模式,只造出一個對象供外界使用;這個枚舉一下造出好多個供使用,造出的對象不能改變

枚舉出來的ABCDE都是可以用類名.直接調用的對象,對象可以賦值,和調用其成員方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.