java练习2

题目要求

  1. 创建一个Color枚举类
  2. 有RED,BLUE,BLACK,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语句中匹配使用

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();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值