How to give enum values that are having space

前端 未结 5 2064
走了就别回头了
走了就别回头了 2021-01-23 22:39

i have to create an enum that contains values that are having spaces

public enum MyEnum
        {
            My cart,
            Selected items,
            Bi         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 23:15

    I agree the use of DisplayText if you are following convention. But if you need different display value to represent the enum constant, then you could have a constructor passing that value.

    public enum MyEnum { My cart ("Cart"), Selected items("All Selected Items"), Bill("Payment");

    private String displayValue; 
    private MyEnum(String displayValue) {
    

    this.displayValue = displayValue; }
    public String displayText() { return this.displayValue; }

    }

    You can have a displayText method or a toString method which would return the displayValue

提交回复
热议问题