与使用 int
或 String
相比,大多数人都明白 enum
给程序带来的先天优势。参见 here和 here如果你不知道。无论如何,我遇到了一个我想解决的问题,这种问题与使用 int
或 String
来表示常量而不是使用 枚举
。这专门处理 String.format(...)。
对于 String.format,似乎有一个很大的程序错误 开口,在编译时找不到。这会使修复错误变得更加复杂和/或需要更长的时间。
这就是我要解决(或破解解决方案)的问题。我接近了,但我还不够接近。对于这个问题,这更肯定是过度设计了。我明白这一点,但我只想为此找到一个好的编译时解决方案,它提供最少的样板代码。
我正在编写一些非生产代码,只是为了编写具有以下规则的代码。
- 抽象是关键。
可读性非常重要
然而,最简单的方法是首选。
我正在运行......
- Java 7/JDK 1.7
- Android Studio 0.8.2
这些都不尽如人意
- Is there a typesafe alternative to String.format(...)
- How to get string.format to complain at compile time
我的解决方案
我的解决方案使用与 enums
相同的想法。任何时候你都应该使用枚举类型来表示一组固定的常量......你在编译时知道所有可能值的数据集(docs.oracle.com)。 String.format
中的第一个参数似乎符合该要求。你事先知道整个字符串,你可以把它分成几个部分(或只是一个部分),所以它可以表示为一组固定的“常量”。
顺便说一句,我的项目是一个您可能已经在网上看到过的简单计算器 - 2 个输入数字、1 个结果和 4 个按钮(+、-、× 和 ÷)。我还有第二个只有 1 个输入数字的重复计算器,但其他一切都一样
枚举 - Expression.java & DogeExpression.java
public enum Expression implements IExpression {
Number1 ("%s"),
Operator (" %s "),
Number2 ("%s"),
Result (" = %s");
protected String defaultFormat;
protected String updatedString = "";
private Expression(String format) { this.defaultFormat = format; }
// I think implementing this in ever enum is a necessary evil. Could use a switch statement instead. But it would be nice to have a default update method that you could overload if needed. Just wish the variables could be hidden.
public <T> boolean update(T value) {
String replaceValue
= this.equals(Expression.Operator)
? value.toString()
: Number.parse(value.toString()).toString();
this.updatedString = this.defaultFormat.replace("%s", replaceValue);
return true;
}
}
...和...
public enum DogeExpression implements IExpression {
Total ("Wow. Such Calculation. %s");
// Same general code as public enum Expression
}
当前问题
IExpression.java - 这是一个巨大问题。如果不修复此问题,我的解决方案将无法工作!!
public interface IExpression {
public <T> boolean update(T Value);
class Update { // I cannot have static methods in interfaces in Java 7. Workaround
public static String print() {
String replacedString = "";
// for (Expression expression : Expression.values()) { // ISSUE!! Switch to this for Expression
for (DogeExpression expression : DogeExpression.values()) {
replacedString += expression.updatedString;
}
return replacedString;
}
}
}
那么为什么这是一个问题
对于 IExpression.java
,这必须破解才能与 Java 7 一起工作。我觉得 Java 8 对我来说会更好。 但是,我遇到的问题是最重要的让我当前的实现工作问题是IExpression
不知道哪个enum
进行迭代。所以我必须评论/取消注释代码才能让它现在工作。
如何解决上述问题?
最佳答案
这样的事情怎么样:
public enum Operator {
addition("+"),
subtraction("-"),
multiplication("x"),
division("÷");
private final String expressed;
private Operator(String expressed) { this.expressed = expressed; }
public String expressedAs() { return this.expressed; }
}
public class ExpressionBuilder {
private Number n1;
private Number n2;
private Operator o1;
private Number r;
public void setN1(Number n1) { this.n1 = n1; }
public void setN2(Number n2) { this.n2 = n2; }
public void setO1(Operator o1) { this.o1 = o1; }
public void setR(Number r) { this.r = r; }
public String build() {
final StringBuilder sb = new StringBuilder();
sb.append(format(n1));
sb.append(o1.expressedAs());
sb.append(format(n2));
sb.append(" = ");
sb.append(format(r));
return sb.toString();
}
private String format(Number n) {
return n.toString(); // Could use java.text.NumberFormat
}
}
关于java - 我正在制作一个安全的、编译时的 String.format(...) 等价物。问题仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24849950/