[英]using enum inside a switch
我們可以在交換機內使用枚舉嗎?
public enum Color {
RED,BLUE,YELLOW
}
public class Use {
Color c = Color.BLUE;
public void test(){
switch(c){
case Color.BLUE:
}
}
}
我在這收到一些錯誤。
The enum constant Color.BLUE reference cannot be qualified in a case label Use.java line 7 Java Problem
case COLOR.BLUE:
}
在上面的代碼而不是COLOR.BLUE只寫藍色
例如
import java.awt.Color;
class ColorEnum {
enum Color{BLUE,RED,YELLOW};
public static void main(String[] args) {
Color c = Color.BLUE;
switch(c) {
case BLUE:
System.out.println("Blue!");
break;
case RED:
System.out.println("Red!");
break;
case YELLOW:
System.out.println("Yellow!");
break;
default:
System.out.println("Logic error!");
}
}
}
寫這樣:
public void test(){
switch(c) {
case BLUE:
}
}
當用作案例標簽時, enum
標簽不得限定。 JLS 14.11的語法說明了這一點:
SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
EnumConstantName:
Identifier
請注意,需要一個簡單的標識符,而不是枚舉名稱限定的標識符。
(我不知道他們為什么設計這樣的語法。可能是為了避免語法中的歧義。但無論如何,這就是它的方式。)
為什么要使用開關? 而只是讓enum持有Color信息本身(封裝它),從而完成所有骯臟的工作。 這樣做的好處是,如果您更改枚舉,則不必通過使用它的所有代碼,更改所有switch語句。 例如:
import java.awt.Color;
public enum MyColor {
RED("Red", Color.red), BLUE("Blue", Color.blue),
YELLOW("Yellow", Color.yellow);
private String text;
private Color color;
private MyColor(String text,Color color) {
this.text = text;
this.color = color;
}
public String getText() {
return text;
}
public Color getColor() {
return color;
}
@Override
public String toString() {
return text;
}
}
以及如何使用它的一個例子如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
class MyColorTest extends JPanel {
private static final Dimension PREF_SIZE = new Dimension(400, 300);
public MyColorTest() {
for (final MyColor myColor : MyColor.values()) {
add(new JButton(new AbstractAction(myColor.getText()) {
@Override
public void actionPerformed(ActionEvent arg0) {
MyColorTest.this.setBackground(myColor.getColor());
}
}));
}
}
@Override
public Dimension getPreferredSize() {
return PREF_SIZE;
}
private static void createAndShowUI() {
JFrame frame = new JFrame("MyColorTest");
frame.getContentPane().add(new MyColorTest());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
是的,您可以在switch語句中使用枚舉,但請確保在案例標簽中不使用FQCN(完全限定類名)。
以下是從“ 枚舉常量引用不能在switch語句的case標簽中限定 ”中提取的
簡而言之
當Java switch語句使用enum參數時; 枚舉值的限定名稱不應用於案例標簽,而應僅用於不合格的名稱; 然后switch語句會認為所有標簽都指的是用作參數的枚舉類型。
為什么只有不合格的價值?
如果允許使用合格的參考文獻標簽; 沒有辦法限制標簽中使用的枚舉類型與switch語句的參數類型相同。
public enum Status {
REGISTERED,
TERMINATED
}
public class TestStatus {
public static String getMessage(Status status) {
String message;
switch (status) {
// case Status.REGISTERED: // line 1
case REGISTERED: // line 2
message = "Welcome";
break;
default:
message = "Good bye";
break;
}
return message;
}
enum MyEnum{
A,B,C
MyEnum(){
switch(this){
case A:
//....
break;
case B:
//....
break;
case C:
//....
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.