Special characters in an enum

前端 未结 3 1353
情话喂你
情话喂你 2020-12-28 08:35

I want to put the special characters, the parentheses ( \'(\' and \')\' ) and the apostrophe (\'), in an enum.

I had this:

private enum specialChars{         


        
3条回答
  •  情话喂你
    2020-12-28 09:14

    Enum constants must be valid Java identifiers. You can override toString if you would like them displayed differently.

    public enum SpecialChars {
    
        OPEN_PAREN {
            public String toString() {
                return "(";
            }
        },
    
        CLOSE_PAREN {
            public String toString() {
                return ")";
            }
        },
    
        QUOTE {
            public String toString() {
                return "'";
            }
        }
    
    }
    

提交回复
热议问题