Enum创建

0.096字数 13阅读 39

Color枚举


package hello;

/**
 * 颜色枚举
 * 
 * @author jarworker
 *
 */
public enum Color {
    yes, 
    none(), 
    Red("#FF0000", "红色"),
    Green("#00FF00", "绿色"), 
    Blue("#0000FF", "蓝色"), 
    black("#000000","黑色"), 
    white("#FFFFFF", "白色");
    private String RGB;
    private String color;

    private Color() {
    }

    private Color(String rGB, String color) {
        RGB = rGB;
        this.color = color;
    }

    public String getRGB() {
        return RGB;
    }

    public void setRGB(String rGB) {
        RGB = rGB;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

}

TestEnum 类(测试用例)

package hello;

import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Test;

public class TestEnum {
    @Test
    public void enmuTest() throws Exception {
        List<Color> list = new ArrayList<>();
        Color colorYes = Color.yes;
        Color colorNone = Color.none;
        Color colorRed = Color.Red;
        Color colorBlue = Color.Blue;
        Color colorGreen = Color.Green;
        Color colorBlack = Color.black;
        Color colorWhite = Color.white;
        list.add(colorYes);
        list.add(colorNone);
        list.add(colorRed);
        list.add(colorBlue);
        list.add(colorGreen);
        list.add(colorBlack);
        list.add(colorWhite);
        // 得出所有的枚举默认值
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i) + "的RGB:" + list.get(i).getRGB() + "   颜色:" + list.get(i).getColor());
        }
        // 设置枚举的值,设置Red
        System.out.println("=================设置Red======================");
        colorRed.setRGB("#FFC0CB");
        colorRed.setColor("pink");
        System.out.println(colorRed + "的RGB:" + colorRed.getRGB() + "   颜色:" + colorRed.getColor());
        // 没有默认值的
        System.out.println("=================没有默认值的======================");
        System.out.println("==========Yes==========");
        colorYes.setRGB("#DB7093");
        colorYes.setColor("苍白的紫罗兰红色");
        System.out.println(colorYes + "的RGB:" + colorYes.getRGB() + "   颜色:" + colorYes.getColor());
        System.out.println("==========None==========");
        colorNone.setRGB("  #FF00FF");
        colorNone.setColor("灯笼海棠(紫红色)");
        System.out.println(colorNone + "的RGB:" + colorNone.getRGB() + "   颜色:" + colorNone.getColor());
    }
}

Console:输出


yes的RGB:null   颜色:null
none的RGB:null   颜色:null
Red的RGB:#FF0000   颜色:红色
Blue的RGB:#0000FF   颜色:蓝色
Green的RGB:#00FF00   颜色:绿色
black的RGB:#000000   颜色:黑色
white的RGB:#FFFFFF   颜色:白色
=================设置Red======================
Red的RGB:#FFC0CB   颜色:pink
=================没有默认值的======================
==========Yes==========
yes的RGB:#DB7093   颜色:苍白的紫罗兰红色
==========None==========
none的RGB:   #FF00FF   颜色:灯笼海棠(紫红色)
PASSED: enmuTest

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@39ed3c8d: 35 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@2401f4c3: 25 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@7d6f77cc: 15 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6504e3b2: 50 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1c4af82c: 61 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms