在没有构造函数的情况下创建 NumberFormat
create NumberFormat without constructor
我有以下 class:
import java.text.NumberFormat;
public static class NF
{
public static NumberFormat formatShares = NumberFormat.getInstance();
public static NumberFormat formatCash = NumberFormat.getInstance();
public NF(){
formatShares.setGroupingUsed(true);
formatShares.setMaximumFractionDigits(0);
formatCash.setGroupingUsed(true);
formatCash.setMaximumFractionDigits(2);
formatCash.setMinimumFractionDigits(2);
}
}
有没有办法做到这一点,这样我就不必实例化 class?本质上我希望能够只使用 NF.formatCash.format(1234567.456)
您可以在静态初始化块中修改您的 NumberFormat
对象:
public static class NF {
public static NumberFormat formatShares = NumberFormat.getInstance();
public static NumberFormat formatCash = NumberFormat.getInstance();
static {
formatShares.setGroupingUsed(true);
formatShares.setMaximumFractionDigits(0);
formatCash.setGroupingUsed(true);
formatCash.setMaximumFractionDigits(2);
formatCash.setMinimumFractionDigits(2);
}
}
当 class 初始化时,初始化块中的代码将 运行,因此不需要创建 NF
的实例来执行您的代码。
您可以将您的 class 变成单身人士。
它不完全是你想要的格式,但它确实满足你的要求,因为你不必自己实例化 class,当你想使用 NF
时它会自动完成。
NF.getInstance().formatCash.format(1234567.456)
你的 class 看起来像这样:
public class NF {
public NumberFormat formatShares = NumberFormat.getInstance();
public NumberFormat formatCash = NumberFormat.getInstance();
private static NF theInstance;
private NF() {
formatShares.setGroupingUsed(true);
formatShares.setMaximumFractionDigits(0);
formatCash.setGroupingUsed(true);
formatCash.setMaximumFractionDigits(2);
formatCash.setMinimumFractionDigits(2);
}
public static NF getInstance() {
if (theInstance == null) {
theInstance = new NF();
}
return theInstance;
}
}
其实是不可能的。您直接或间接地创建了至少一个 NumberFormat
实例。您可以减少这些实例的数量。
使用static
对象:
public static final NumberFormat formatShares = NumberFormat.getInstance();
static {
formatShares.setGroupingUsed(true);
formatShares.setMaximumFractionDigits(0);
}
这对于多线程是不正确的,因为 NumberFormat
没有保存线程。
使用ThreadLocal
在每个线程的实例上使用:
public static final ThreadLocal<NumberFormat> threadLocalFormatShares = ThreadLocal.withInitial(() -> {
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(true);
nf.setMaximumFractionDigits(0);
return nf;
});
NumberFormat formatShares = threadLocalFormatShares.get();
我认为这可以解决您的问题。
我有以下 class:
import java.text.NumberFormat;
public static class NF
{
public static NumberFormat formatShares = NumberFormat.getInstance();
public static NumberFormat formatCash = NumberFormat.getInstance();
public NF(){
formatShares.setGroupingUsed(true);
formatShares.setMaximumFractionDigits(0);
formatCash.setGroupingUsed(true);
formatCash.setMaximumFractionDigits(2);
formatCash.setMinimumFractionDigits(2);
}
}
有没有办法做到这一点,这样我就不必实例化 class?本质上我希望能够只使用 NF.formatCash.format(1234567.456)
您可以在静态初始化块中修改您的 NumberFormat
对象:
public static class NF {
public static NumberFormat formatShares = NumberFormat.getInstance();
public static NumberFormat formatCash = NumberFormat.getInstance();
static {
formatShares.setGroupingUsed(true);
formatShares.setMaximumFractionDigits(0);
formatCash.setGroupingUsed(true);
formatCash.setMaximumFractionDigits(2);
formatCash.setMinimumFractionDigits(2);
}
}
当 class 初始化时,初始化块中的代码将 运行,因此不需要创建 NF
的实例来执行您的代码。
您可以将您的 class 变成单身人士。
它不完全是你想要的格式,但它确实满足你的要求,因为你不必自己实例化 class,当你想使用 NF
时它会自动完成。
NF.getInstance().formatCash.format(1234567.456)
你的 class 看起来像这样:
public class NF {
public NumberFormat formatShares = NumberFormat.getInstance();
public NumberFormat formatCash = NumberFormat.getInstance();
private static NF theInstance;
private NF() {
formatShares.setGroupingUsed(true);
formatShares.setMaximumFractionDigits(0);
formatCash.setGroupingUsed(true);
formatCash.setMaximumFractionDigits(2);
formatCash.setMinimumFractionDigits(2);
}
public static NF getInstance() {
if (theInstance == null) {
theInstance = new NF();
}
return theInstance;
}
}
其实是不可能的。您直接或间接地创建了至少一个 NumberFormat
实例。您可以减少这些实例的数量。
使用static
对象:
public static final NumberFormat formatShares = NumberFormat.getInstance();
static {
formatShares.setGroupingUsed(true);
formatShares.setMaximumFractionDigits(0);
}
这对于多线程是不正确的,因为 NumberFormat
没有保存线程。
使用ThreadLocal
在每个线程的实例上使用:
public static final ThreadLocal<NumberFormat> threadLocalFormatShares = ThreadLocal.withInitial(() -> {
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(true);
nf.setMaximumFractionDigits(0);
return nf;
});
NumberFormat formatShares = threadLocalFormatShares.get();
我认为这可以解决您的问题。