抽象类 NumberFormat - 对 getInstance() 非常困惑

发布于 2024-08-25 04:38:14 字数 414 浏览 5 评论 0原文

我是 Java 新手,有一个初学者问题:

NumberFormat 是一个抽象类,因此我认为我无法创建它的实例。但是有一个公共静态(工厂?)方法 getInstance() 允许我做

NumberFormat nf = NumberFormat.getInstance();  

我很困惑的事情。如果有人能给我提示,我会很高兴:

  1. 如果有一个公共方法来获取这个抽象类的实例,为什么我们没有一个构造函数呢?
  2. 这是一个抽象类;我们怎样才能让这个静态方法给我们一个类的实例呢?
  3. 为什么选择这样的设计?如果我假设可能有一个抽象类的实例(???),我根本不明白为什么这个类应该是抽象的。

谢谢。

I'm new to Java and I have a beginner question:

NumberFormat is an abstract class and so I assume I can't make an instance of it. But there is a public static (factory?) method getInstance() that allow me to do

NumberFormat nf = NumberFormat.getInstance();  

I'm quite confuse. I'll be glad if someone could give me hints on:

  1. If there is a public method to get an instance of this abstract class, why don't we have also a constructor?
  2. This is an abstract class ; how can we have this static method giving us an instance of the class?
  3. Why choosing such a design? If I assume it's possible to have an instance of an abstract class (???), I don't get why this class should be abstract at all.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳男子 2024-09-01 04:38:14
  1. 该类是抽象的,因为它是 Java 中每种数字格式的基类(例如,包括 DecimalFormat)。对于本质上未知的数字格式有一个构造函数是毫无用处的。
  2. getInstance() 方法是所谓的工厂方法。它返回当前区域设置的匹配数字格式。由于在编译时不知道需要哪种子类,因此它返回一个 NumberFormat,但是,实例 本身将属于子类型,显然(因为您无法创建抽象类的实例)。
  3. 这种设计使您能够以某种方式灵活地确定要在运行时返回的正确子类实例,而无需在设计/编译时使该设计过于僵化。静态方法不属于抽象方法,因此类可以同时充当工厂和具体实现的抽象超类型。如果不是这种情况,您可能会在某个地方有一个包含工厂方法的 NumberFormatFactory
  1. The class is abstract because it is the base class for every number format in Java (this includes DecimalFormat, for example). Having a constructor for an essentially unknown number format is pretty useless.
  2. The getInstance() method is a so-called factory method. It returns a matching number format for the current locale. Since it is not known what kind of sub-class is required at compile-time, it returns a NumberFormat, however, the instance itself, will be of a sub-type, obviously (since you can't create instances of abstract classes).
  3. This design gives you the flexibility of somehow determining the proper subclass instance to return at runtime without making too much of that design rigid at design/compile time. Static methods are exempt from being abstract so a class can work as both a factory and an abstract supertype for concrete implementations. If this weren't the case you'd probably have a NumberFormatFactory somewhere which would have the factory methods.
心凉怎暖 2024-09-01 04:38:14

实际上,您可以从中获取

public static final NumberFormat getInstance()

ALSO一个NumberFormat,但它是它的子类的具体实例。

无论如何,您都无法实例化抽象类,因此该方法不能返回普通的 NumberFormat,而只能返回 至少 NumberFormat 的内容。在这种情况下,该方法用于获取您的语言环境的默认格式化程序,该格式化程序可能是 DecimalFormat 或其某些变体。

DecimalFormat 的文档中,它指出:

获取 NumberFormat
特定区域设置,包括默认区域设置
区域设置,调用 NumberFormat 之一
工厂方法,例如
getInstance()。一般情况下不要打电话
DecimalFormat 构造函数
直接,因为 NumberFormat
工厂方法可能返回子类
除了 DecimalFormat 之外。

最后:你可以确定,如果它是抽象的,那么你无法实例化它,Java 本身也不能......因为声明缺少一些部分,所以它是一个真正的不完整类型。

Actually what you can obtain from

public static final NumberFormat getInstance()

is something that is ALSO a NumberFormat, but it is a concrete instance of a subclass of it.

You can't in anyway instantiate an abstract class so that method can't return a plain NumberFormat but something that is at least a NumberFormat. In this case the method is used to obtain a default formatter for your locale that will probably be a DecimalFormat or some variations of it

In the documentation of DecimalFormat it states:

To obtain a NumberFormat for a
specific locale, including the default
locale, call one of NumberFormat's
factory methods, such as
getInstance(). In general, do not call
the DecimalFormat constructors
directly, since the NumberFormat
factory methods may return subclasses
other than DecimalFormat.

To end: you can be sure, if it's abstract, then you cannot instantiate it, neither Java itself can.. since the declaration is missing some parts so it's a real incomplete type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文