Java:静态类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1844355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Static Class?
提问by Nick Heiner
I have a class full of utility functions. Instantiating an instance of it makes no semantic sense, but I still want to call its methods. What is the best way to deal with this? Static class? Abstract?
我有一个充满实用功能的课程。实例化它的实例没有语义意义,但我仍然想调用它的方法。处理这个问题的最佳方法是什么?静态类?抽象的?
采纳答案by David Robles
Private constructor and static methods on a class marked as final.
标记为 final 的类上的私有构造函数和静态方法。
回答by Bill the Lizard
There's no point in declaring the class as static
. Just declare its methods static
and call them from the class name as normal, like Java's Mathclass.
将类声明为static
. 只需声明它的方法static
并像往常一样从类名调用它们,就像 Java 的Math类一样。
Also, even though it isn't strictly necessary to make the constructor private, it is a good idea to do so. Marking the constructor private prevents other people from creating instances of your class, then calling static methods from those instances. (These calls work exactly the same in Java, they're just misleading and hurt the readability of your code.)
此外,即使将构造函数设为私有并不是绝对必要的,但这样做也是一个好主意。将构造函数标记为私有可防止其他人创建您的类的实例,然后从这些实例调用静态方法。(这些调用在 Java 中的工作方式完全相同,它们只是误导并损害了代码的可读性。)
回答by David Robles
According to the great book "Effective Java":
根据伟大的书“Effective Java”:
Item 4: Enforce noninstantiability with a private constructor
第 4 项:使用私有构造函数强制不可实例化
- Attempting to enforce noninstantiability by making a class abstract does not work.
- 试图通过使类抽象来强制不可实例化是行不通的。
- A default constructor is generated only if a class contains no explicit constructors, so a class can be made noninstantiable by including a private constructor:
- 仅当类不包含显式构造函数时才会生成默认构造函数,因此可以通过包含私有构造函数使类不可实例化:
// Noninstantiable utility class
public class UtilityClass
{
// Suppress default constructor for noninstantiability
private UtilityClass() {
throw new AssertionError();
}
}
Because the explicit constructor is private, it is inaccessible outside of the class. The AssertionError isn't strictly required, but it provides insurance in case the constructor is accidentally invoked from within the class. It guarantees that the class will never be instantiated under any circumstances. This idiom is mildly counterintuitive, as the constructor is provided expressly so that it cannot be invoked. It is therefore wise to include a comment, as shown above.
因为显式构造函数是私有的,所以在类之外是不可访问的。AssertionError 不是严格要求的,但它提供了在类中意外调用构造函数的情况下的保险。它保证该类在任何情况下都不会被实例化。这个习惯用法有点违反直觉,因为构造函数是明确提供的,因此不能被调用。因此,如上所示,包含注释是明智的。
As a side effect, this idiom also prevents the class from being subclassed. All constructors must invoke a superclass constructor, explicitly or implicitly, and a subclass would have no accessible superclass constructor to invoke.
作为副作用,这个习语还阻止类被子类化。所有构造函数都必须显式或隐式调用超类构造函数,子类将没有可访问的超类构造函数来调用。
回答by Bennett Dill
Just to swim upstream, static members and classes do not participate in OO and are therefore evil. No, not evil, but seriously, I would recommend a regular class with a singleton pattern for access. This way if you need to override behavior in any cases down the road, it isn't a major retooling. OO is your friend :-)
只是为了向上游游,静态成员和类不参与面向对象,因此是邪恶的。不,不是邪恶的,但说真的,我会推荐一个带有单例模式的常规类来访问。这样,如果您在任何情况下都需要覆盖行为,这不是主要的重组。OO 是你的朋友 :-)
My $.02
我的 $.02
回答by irreputable
comment on the "private constructor" arguments: come on, developers are not that stupid; but they ARE lazy. creating an object then call static methods? not gonna happen.
评论“私有构造函数”参数:来吧,开发人员没那么蠢;但他们很懒。创建一个对象然后调用静态方法?不会发生。
don't spend too much time to make sure your class cannot be misused. have some faith for your colleagues. and there is always a way to misuse your class no matter how you protect it. the only thing that cannot be misused is a thing that is completely useless.
不要花太多时间来确保您的类不会被滥用。对你的同事有一些信心。无论你如何保护它,总有一种方法可以滥用你的类。唯一不能滥用的东西,就是完全没用的东西。
回答by fastcodejava
- Final class and private constructor (good but not essential)
- Public static methods
- 最终类和私有构造函数(好但不是必需的)
- 公共静态方法
回答by crowne
Sounds like you have a utility class similar to java.lang.Math.
The approach there is final class with private constructor and static methods.
听起来您有一个类似于java.lang.Math的实用程序类。
方法是具有私有构造函数和静态方法的最终类。
But beware of what this does for testability, I recommend reading this article
Static Methods are Death to Testability
但是要注意这对可测试性的影响,我建议阅读这篇文章
静态方法是可测试性的死亡
回答by user811602
You can use @UtilityClass annotation from lombok https://projectlombok.org/features/experimental/UtilityClass
您可以使用来自 lombok https://projectlombok.org/features/experimental/UtilityClass 的@UtilityClass 注释