- package com;
- enum Color{
- RED,GREEN,BLUE;
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- switch(this){
- case RED:{
- if("红色".equals(RED)){
- this.name = name;
- }else{
- System.out.println("设置内容错误。");
- }
- break;
- }
- case GREEN:{
- if("绿色".equals(GREEN)){
- this.name = name;
- }else{
- System.out.println("设置内容错误。");
- }
- break;
- }
- case BLUE:{
- if("蓝色".equals(BLUE)){
- this.name = name;
- }else{
- System.out.println("设置内容错误。");
- }
- break;
- }
- }
- }
-
- }
- public class SetEnum {
- public static void main(String[] args) {
- Color c = Enum.valueOf(Color.class, "BLUE");
- //Color c = Color.BLUE;
- c.setName("兰色");
- c.setName("蓝色");
- System.out.println(c.getName());
- }
- }
复制代码 提示NUll什么回事。
|
|