我有以下课程:

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);
}
}

反正有没有这样做,所以我没有实例化课程?基本上我希望能够使用NF.formatCash.format(1234567.456)

3回答

  • 0
    1 年前

    实际上这是不可能的 . 直接或间接创建至少一个 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();

    我认为这可以解决你的问题 .


  • 0
    1 年前

    您可以在静态初始化块中修改 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);
    }
    }

    初始化类时初始化块中的代码将运行,因此不需要为您的代码创建 NF 实例 .


  • 3
    1 年前

    你可以将你的 class 变成一个单身人士 .

    当你想使用 NF 时,它会自动完成's not entirely the format you want but it does meet your requirements in that you do not have to instantiate the class yourself, it' .

    NF.getInstance().formatCash.format(1234567.456)

    那么你的课就像这样:

    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的更多相关文章

  1. JavaScript对象的创建之构造函数

    通过构造函数的方式创建和基于工厂的创建类似,最大的区别就是函数的名称就是类的名称,按照java的约定,第一个字母大写. 使用构造函数创建对象时,在函数内部是通过this关键字来完成属性的定义. fun ...

  2. 为my_string类创建复制构造函数copy constructor ,拷贝函数名和类同名

    为下面的my_string类创建一个复制构造函数,并将定义该类的代码提交. my_string类的定义: class my_string { char *s; public: my_string(ch ...

  3. vc++如何创建程序-构造函数

    如果给Animal带参,则提示没有缺省的构造函数了,缺省就是不带参数的 改进:从子类当中向基类传递代参的,这样他就会给Animal传递400,300 对一个常量来调用 #include<iost ...

  4. vc++如何创建程序-构造函数02

    1.若忘记了赋值,出现运行结果是很大的负值(因为我们定义的x与y这两个成员变量存储在内存中是一个随机的值) 当我们调用时,随机输出. //包含输入输出的头文件#include<iostream. ...

  5. c++ 2.1 编译器何时创建默认构造函数

    我们通常会说当生命一个 class 时,如果我们不为该 class 指定一个 constructor,那么编译器会替我们实现一个 connstructor,那么这种说法一定对吗? 事实上,这是不对的. ...

  6. JavaScript之面向对象学习七(动态原型模式、寄生构造函数模式、稳妥构造函数模式创建自定义类型)

    一.动态原型模式 在面向对象学习六中的随笔中,了解到组合构造函数模式和原型模式创建的自定义类型可能最完善的!但是人无完人,代码亦是如此! 有其他oo语言经验的开发人员在看到独立的构造函数和原型时,很可 ...

  7. C++构造函数和析构函数

    构造函数简介 在上一个章节我们在创建好类的对象之后,首先对它的每一个成员属性赋值之后再对它们进行输出操作,如果不赋值就输出,这些值就会是垃圾值.而为了代码的简介,一次性为所有成员属性初始化,C++的类 ...

  8. JS使构造函数与new操作符无关

    function User(name, passwordHash) { this.name = name; this.passwordHash = passwordHash; } 当使用User函数创 ...

  9. C++中默认构造函数中数据成员的初始化

    构造函数的任务是初始化数据成员的,在类中,如果没有显示定义任何构造函数,编译器将为我们创建一个构造函数,称为合成的默认构造函数,合成的默认构造函数使用与变量初始化相同的规则来初始化成员.即当类中的数据 ...

  10. C++构造函数与析构函数

    转自http://blog.csdn.net/tqtuuuu/article/details/6652144 构造函数 对于C++的构造函数,暂且将其分为以下几类: 1. 默认构造函数 2. 隐士转换 ...

随机推荐

  1. 神经网络之卷积篇:详解残差网络(ResNets)(Residual Networks (ResNets))

    详解残差网络 ResNets是由残差块(Residual block)构建的,首先解释一下什么是残差块. 这是一个两层神经网络,在\(L\)层进行激活,得到\(a^{\left\lbrack l + ...

  2. KubeSphere v4 开源并发布全新可插拔架构 LuBan

    2024 年 10 月 10 日,KubeSphere 开源社区激动地向大家宣布,KubeSphere v4(开源版)已正式发布,同时发布全新可插拔架构 KubeSphere LuBan. 相较于 K ...

  3. KubeKey v3.1 发布:快速自定义离线安装包

    日前,KubeKey v3.1 正式发布.该版本主要对离线场景部署.离线包制作以及向 Kubernetes v1.24+ 升级进行了优化. KubeKey 简介 KubeKey 是 KubeSpher ...

  4. 基于Jenkins + Argo 实现多集群的持续交付

    作者:周靖峰,青云科技容器顾问,云原生爱好者,目前专注于 DevOps,云原生领域技术涉及 Kubernetes.KubeSphere.Argo. 前文概述 前面我们已经掌握了如何通过 Jenkins ...

  5. KubeSphere 开源社区 2022 年度回顾与致谢

    2022 年,国内的云原生技术生态日趋完善,细分技术项目也不断涌现,形成了完整的支撑应用云原生化的全生命周期技术体系.基础设施即代码.微服务.Serverless 等技术,促使基础设施资源向更加灵活弹 ...

  6. 【2024.09.15】NOIP2024 赛前集训(2)

    [2024.09.15]NOIP2024 赛前集训(2) A 最大的难点戏剧性地变成了二叉搜索树是什么. 先根据已知序列把二叉树建出来,忘了二叉搜索树的移步 二叉搜索树 & 平衡树 - OI ...

  7. vite3+vue3 实现前端部署加密混淆 javascript-obfuscator

    安装 pnpm install javascript-obfuscator 安装之后 在项目根目录新建一个 obfuscator.js 在 obfuscator.js 写入以下代码 直接复制粘贴 ` ...

  8. Flink 实战之从 Kafka 到 ES

    Flink 实战系列 -- 从 Kafka 到 ES [Flink 实战系列]通过一个个简单的例子,图解分析 Flink 的底层原理. 做个数据搬运工 本例的场景非常常见:消费 Kafka 的数据写入 ...

  9. Python:pygame游戏编程之旅四(游戏界面文字处理)

    本节讲解游戏界面中字体的处理,以在界面中实时显示当前时间.小球位置为例进行实验,具体见代码. 一.代码 # -*- coding:utf-8 -*- import os import sys impo ...

  10. golang之浮点数处理库decimal

    decimal库包是用来解决float类型对象之间运算不准确的问题的.所以,如果你想使用decimal库包,你必须先把float类型对象通过decimal.NewFromFloat()函数转成deci ...