Java 由静态final类实现的抽象字段?

Java 由静态final类实现的抽象字段?,java,android,field,abstract-class,color-palette,Java,Android,Field,Abstract Class,Color Palette,我正在尝试为我的Android应用程序创建自定义调色板。我开始这样做: public class Theme { public static final class DARK { final static int POSITIVE_GREEN = Color.rgb(0, 200, 0); final static int NEGATIVE_RED = Color.rgb(200, 0, 0); final static int BACK

我正在尝试为我的Android应用程序创建自定义调色板。我开始这样做:

public class Theme {

    public static final class DARK {
        final static int POSITIVE_GREEN = Color.rgb(0, 200, 0);
        final static int NEGATIVE_RED = Color.rgb(200, 0, 0);
        final static int BACKGROUND_GREEN = Color.argb(255 - 220, 0, 255, 0); //220
        final static int BACKGROUND_RED = Color.argb(255 - 220, 255, 0, 0);
    }

    public static final class PASTEL {
        final static int POSITIVE_GREEN = Color.parseColor("#326262");
        final static int NEGATIVE_RED = etc
        final static int BACKGROUND_GREEN = etc
        final static int BACKGROUND_RED = etc
    }

}
但当我想重构其中一个字段名时,我意识到有些地方不对劲。字段名在主题或其他方面应该是抽象的,但由于这些不应该是可实例化的类,因此我不能使用此处建议的构造函数技巧:
我应该如何做到这一点?

在这里使用接口似乎是合适的:

每个主题都将实现此接口:

public interface Theme {
    int getPositiveGreen(); 
    ...
}
例如:

public class Dark implements Theme {

   private final static int POSITIVE_GREEN = Color.rgb(0, 200, 0);

   public int getPositiveGreen() {
       return POSITIVE_GREEN;
   }
   ....
}
public enum DARK {
        POSITIVE_GREEN (0,200, ..){
              void abstractField(){
                 //Achieve your custom methods
              }
        },
        NEGATIVE_RED (200,0, ..){
           //Achieve your custom methods
        },
        BACKGROUND_GREEN (.....){
           //Achieve your custom methods
        },
        BACKGROUND_RED (...){
          //Achieve your custom methods
        },
      

       // Offer (with the Senate) constructor to initialize the fields defined
       DARK (int variable1, int variable2) {
                this.variable1 = variable1;
                this.variable2 = variable2;
       }
       // Define the fields you need
       private int variable1;
       private int variable2;

       //Custom abstract methods you need
       abstract void abstartField();
}
一般来说,公共静态变量的使用应该局限于一些非常简单的场景,而这并不是您想要实现的


在这里使用接口似乎合适:

每个主题都将实现此接口:

public interface Theme {
    int getPositiveGreen(); 
    ...
}
例如:

public class Dark implements Theme {

   private final static int POSITIVE_GREEN = Color.rgb(0, 200, 0);

   public int getPositiveGreen() {
       return POSITIVE_GREEN;
   }
   ....
}
public enum DARK {
        POSITIVE_GREEN (0,200, ..){
              void abstractField(){
                 //Achieve your custom methods
              }
        },
        NEGATIVE_RED (200,0, ..){
           //Achieve your custom methods
        },
        BACKGROUND_GREEN (.....){
           //Achieve your custom methods
        },
        BACKGROUND_RED (...){
          //Achieve your custom methods
        },
      

       // Offer (with the Senate) constructor to initialize the fields defined
       DARK (int variable1, int variable2) {
                this.variable1 = variable1;
                this.variable2 = variable2;
       }
       // Define the fields you need
       private int variable1;
       private int variable2;

       //Custom abstract methods you need
       abstract void abstartField();
}
一般来说,公共静态变量的使用应该局限于一些非常简单的场景,而这并不是您想要实现的


使用枚举而不是int常量 您可以改为使用枚举,例如:

public class Dark implements Theme {

   private final static int POSITIVE_GREEN = Color.rgb(0, 200, 0);

   public int getPositiveGreen() {
       return POSITIVE_GREEN;
   }
   ....
}
public enum DARK {
        POSITIVE_GREEN (0,200, ..){
              void abstractField(){
                 //Achieve your custom methods
              }
        },
        NEGATIVE_RED (200,0, ..){
           //Achieve your custom methods
        },
        BACKGROUND_GREEN (.....){
           //Achieve your custom methods
        },
        BACKGROUND_RED (...){
          //Achieve your custom methods
        },
      

       // Offer (with the Senate) constructor to initialize the fields defined
       DARK (int variable1, int variable2) {
                this.variable1 = variable1;
                this.variable2 = variable2;
       }
       // Define the fields you need
       private int variable1;
       private int variable2;

       //Custom abstract methods you need
       abstract void abstartField();
}

我不知道您是否可以帮助使用enum而不是int常量 您可以改为使用枚举,例如:

public class Dark implements Theme {

   private final static int POSITIVE_GREEN = Color.rgb(0, 200, 0);

   public int getPositiveGreen() {
       return POSITIVE_GREEN;
   }
   ....
}
public enum DARK {
        POSITIVE_GREEN (0,200, ..){
              void abstractField(){
                 //Achieve your custom methods
              }
        },
        NEGATIVE_RED (200,0, ..){
           //Achieve your custom methods
        },
        BACKGROUND_GREEN (.....){
           //Achieve your custom methods
        },
        BACKGROUND_RED (...){
          //Achieve your custom methods
        },
      

       // Offer (with the Senate) constructor to initialize the fields defined
       DARK (int variable1, int variable2) {
                this.variable1 = variable1;
                this.variable2 = variable2;
       }
       // Define the fields you need
       private int variable1;
       private int variable2;

       //Custom abstract methods you need
       abstract void abstartField();
}

我不知道您是否可以帮助

抽象方法只是子类需要提供的函数

抽象字段也可以被认为是需要提供的值

public class Theme {

    public final int POSITIVE_GREEN;
    public final int NEGATIVE_RED;
    public final int BACKGROUND_GREEN;
    public final int BACKGROUND_RED;

    private Theme(int POSITIVE_GREEN, int NEGATIVE_RED, int BACKGROUND_GREEN, int BACKGROUND_RED)
    {
        this.POSITIVE_GREEN = POSITIVE_GREEN;
        this.NEGATIVE_RED = NEGATIVE_RED;
        this.BACKGROUND_GREEN = BACKGROUND_GREEN;
        this.BACKGROUND_RED = BACKGROUND_RED;
    }

    public static final Theme DARK = new Theme(
        Color.rgb(0, 200, 0),
        Color.rgb(200, 0, 0),
        Color.argb(255 - 220, 0, 255, 0),
        Color.argb(255 - 220, 255, 0, 0)
    );

    public static final Theme PASTEL = new Theme( ...

抽象方法只是子类需要提供的函数

抽象字段也可以被认为是需要提供的值

public class Theme {

    public final int POSITIVE_GREEN;
    public final int NEGATIVE_RED;
    public final int BACKGROUND_GREEN;
    public final int BACKGROUND_RED;

    private Theme(int POSITIVE_GREEN, int NEGATIVE_RED, int BACKGROUND_GREEN, int BACKGROUND_RED)
    {
        this.POSITIVE_GREEN = POSITIVE_GREEN;
        this.NEGATIVE_RED = NEGATIVE_RED;
        this.BACKGROUND_GREEN = BACKGROUND_GREEN;
        this.BACKGROUND_RED = BACKGROUND_RED;
    }

    public static final Theme DARK = new Theme(
        Color.rgb(0, 200, 0),
        Color.rgb(200, 0, 0),
        Color.argb(255 - 220, 0, 255, 0),
        Color.argb(255 - 220, 255, 0, 0)
    );

    public static final Theme PASTEL = new Theme( ...

如果您只想使用名称契约,可以使用以下概念

public interface Theme {
    public int POSITIVE_GREEN;
    public int NEGATIVE_RED;
    public int BACKGROUND_GREEN;
    public int BACKGROUND_RED;
    public Color getColor(int colorName);
}

public static final class Theme1 extends Theme {
    Map<Integer, Color> colorMap = new HashMap<>();
    private Theme1() {
        colorMap.put(POSITIVE_GREEN, Color.rgb(0, 200, 0));
        colorMap.put(NEGATIVE_RED, Color.rgb(200, 0, 0));
        colorMap.put(BACKGROUND_GREEN, Color.argb(255 - 220, 0, 255, 0));
        colorMap.put(BACKGROUND_RED, Color.argb(255 - 220, 255, 0, 0));
    }

    @Override
    public Color getColor(int colorName) {
        return colorMap.get(colorName);
    }
}

如果您只想使用名称契约,可以使用以下概念

public interface Theme {
    public int POSITIVE_GREEN;
    public int NEGATIVE_RED;
    public int BACKGROUND_GREEN;
    public int BACKGROUND_RED;
    public Color getColor(int colorName);
}

public static final class Theme1 extends Theme {
    Map<Integer, Color> colorMap = new HashMap<>();
    private Theme1() {
        colorMap.put(POSITIVE_GREEN, Color.rgb(0, 200, 0));
        colorMap.put(NEGATIVE_RED, Color.rgb(200, 0, 0));
        colorMap.put(BACKGROUND_GREEN, Color.argb(255 - 220, 0, 255, 0));
        colorMap.put(BACKGROUND_RED, Color.argb(255 - 220, 255, 0, 0));
    }

    @Override
    public Color getColor(int colorName) {
        return colorMap.get(colorName);
    }
}

您是否尝试过为此查找枚举?您不能强制开发人员重写某个字段。但是,您可以强制他们重写某个方法。您尝试过为此查找枚举吗?您不能强制开发人员重写某个字段。但是,您可以强制它们重写一个方法。