题目要求
- 创建一个Color枚举类
- 有RED,BLUE,BLACK,YELLOW,GREEN这五个枚举值/对象
- Color有三个属性redValue,greenValue,blueValue
- 创建构造方法,参数包括这三个属性
- 每个枚举值都要给这三个属性赋值,三个属性对应的值分别是
- red:255,0,0 blue:0,0,255 black:0,0,0 yellow:255,255,0 green:0,255,0
- 定义接口,里面有方法show,要求Color实现该接口
- show方法中显示三属性的值
- 将枚举对象在switch语句中匹配使用
java
package com.shedu.homework;
public class Homework08 {
public static void main(String[] args) {
Color[] colors = Color.values();
for (Color color: colors){
System.out.print(color);
color.show();
}
}
}
//1.创建一个Color枚举类
enum Color implements ShowValue{
//2.有RED,BLUE,BLACK,YELLOW,GREEN这五个枚举值/对象
//6.red:255,0,0 blue:0,0,255 black:0,0,0 yellow:255,255,0 green:0,255,0
RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),
YELLOW(255,255,0),GREEN(0,255,0);
//3.Color有三个属性redValue,greenValue,blueValue
private int renValue;
private int greenValue;
private int blueValue;
//4.创建构造方法,参数包括这三个属性
private Color(int renValue, int greenValue, int blueValue) {
this.renValue = renValue;
this.greenValue = greenValue;
this.blueValue = blueValue;
}
public int getRenValue() {
return renValue;
}
public int getGreenValue() {
return greenValue;
}
public int getBlueValue() {
return blueValue;
}
@Override
public void show() {
System.out.println("属性为:"+renValue+","+greenValue+","+blueValue);
}
}
interface ShowValue{
public void show();
}