[英]Abstract fields implemented by static final class?
I'm trying to create custom color palettes for my Android app. 我正在尝试为我的Android应用程序创建自定义调色板。 I started doing it this way:
我以这种方式开始:
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
}
}
but realized something was amiss when I wanted to refactor one of the field names. 但是当我想重构一个字段名称时意识到有些不对劲。 The field names should be abstract in THEME or something, but since these shouldn't be instantiable classes, I can't use the constructor trick suggested here: Why not abstract fields?
字段名称应该在THEME或某些内容中是抽象的,但是由于这些名称不应该是可实例化的类,因此我无法使用此处建议的构造方法: 为什么不抽象字段? How should I be doing this?
我应该怎么做?
Use of interfaces seems apppropriate here: 在这里使用接口似乎是合适的:
Every theme will implement this interface: 每个主题都将实现此接口:
public interface Theme {
int getPositiveGreen();
...
}
For example: 例如:
public class Dark implements Theme {
private final static int POSITIVE_GREEN = Color.rgb(0, 200, 0);
public int getPositiveGreen() {
return POSITIVE_GREEN;
}
....
}
In general use of public static variable should be limited to some very simple scenarios which is not what you are trying to achieve. 通常,公共静态变量的使用应限于某些非常简单的方案,而这不是您要达到的目的。
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
Use enum instead of int constants You can use enum instead, for example: 使用enum代替int常量您可以代替使用enum,例如:
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();
}
I do not know if you can help 不知道你能不能帮忙
An abstract method is just a function that the subclass needs to provide. 抽象方法只是子类需要提供的功能。
An "abstract field" can be thought as a value that needs to be provided too 也可以将“抽象字段”视为需要提供的值
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( ...
If you just want a contract of names to be used, you can use below concept. 如果您只想使用名称合同,则可以使用以下概念。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.