【JAVA学习笔记】46 - (43)第十一章作业

项目代码

https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter11/src/com/yinhai/homework11

1.枚举类

1.创建一个Color枚举类

2.有RED,BLUE,BL ACK,YELLOW,GREEN这个五个枚举值/对象:

3. Color有三 个属性redValue, greenValue, blueValue,

4.创建构造方法,参数包括这三个居性,

5.每个枚举值都要给这三个属性赋值,三个属性对应的值分别是

6. red: 255.0,0 blue:0,0,255 black:0,0,0 yellow:255,255,0 green:0,255,0

7.定义接口,里面有方法show,要求Color实现该接口

8. show方法中显示三属性的值

9.将枚举对象在switch语句中四配使用
 

easy,just do exercise

/**
 * @author 银海
 * @version 1.0
 */
public class Homework01 {
    public static void main(String[] args) {
        IMyColor iMyColor = Color.BLACK;//没有想出来接口除了这里能用还有哪里能用
        //也没有啥好处啊,方便以后拓展SRGB?或者HSB?但颜色范围也对不上啊,不懂
        iMyColor.show();
        switch ((Color)iMyColor){
            case RED:
                System.out.println("匹配为红色");
                break;
            case BLUE:
                System.out.println("匹配为蓝色");
                break;
            case BLACK:
                System.out.println("匹配为黑色");
                break;
            case GREEN:
                System.out.println("匹配为绿色");
                break;
            case YELLOW:
                System.out.println("匹配为黄色");
                break;
            default:
                System.out.println("啥都不是");
        }
    }
}
interface IMyColor{
    void show();
}
enum Color implements IMyColor{
    RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);
    private int redValue;
    private int greenValue;
    private int blueValue;

    Color(int redValue, int greenValue, int blueValue) {
        this.redValue = redValue;
        this.greenValue = greenValue;
        this.blueValue = blueValue;
    }
    public void show(){
        System.out.println(redValue + " " + greenValue + " " + blueValue);
    }
}

你可能感兴趣的:(学习,笔记,java)