Java基礎-列舉enum和switch的用法
阿新 • • 發佈:2022-03-08
package com.hspedu.homework_; public class Course441 { public static void main(String[] args) { Color green = Color.GREEN; green.show(); Color red = Color.RED; // 列舉enum和switch的用法 switch(xxx)給定一個列舉物件 switch (red) { 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: break; } } } interface IShow { public abstract void show(); } enum Color implements IShow { 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; } @Override public void show() { System.out.print("red = " + redValue); System.out.print("\tgreen = " + greenValue); System.out.println("\tblue = " + blueValue); System.out.println("=========================="); } }