Java - How to set String as static int

后端 未结 4 551
予麋鹿
予麋鹿 2021-01-28 08:24

I have a method that accepts only a String.

public void setVerticalAlignment(String align) {
            ...
    gd.verticalAlignment = align;   // accepts only          


        
4条回答
  •  梦毁少年i
    2021-01-28 09:06

    Sounds like you want a map:

    // Ideally use ImmutableMap from Guava
    private static final Map ALIGNMENTS = mapAlignments();
    
    private static final Map mapAlignments() {
        Map ret = new HashMap();
        ret.put ("SWT.TOP", SWT.TOP);
        // etc
        return ret;
    }
    

    Then you can just fetch from the map (and unbox) later.

    Or, better, change your method declaration to avoid this in the first place :)

提交回复
热议问题