java 实用程序类 - 正确的方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12538487/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:18:21  来源:igfitidea点击:

Utility Class - What is the correct approach?

javaclass

提问by Shreyos Adikari

What is the correct approach for a utility class having all methods with public static.
Should I use final class or abstract class?
Please give suggestion.
As for example:

对于具有公共静态的所有方法的实用程序类,正确的方法是什么。
我应该使用最终类还是抽象类?
请给出建议。
例如:

public final class A{ 
    public static void method(){
        /* ... */
    }
}

OR

或者

public abstract class A{
    public static void method(){
        /* ... */
    }
}

采纳答案by kosa

abstracthas its own purpose. If you want some of the class functionality implemented by other classes (override) then you use abstract.

abstract有它自己的目的。如果您希望其他类 ( override)实现某些类功能,那么您可以使用抽象。

If it is just utility class, but you don't want other classes subclass it, then I would go with finalclass. If utility class has just staticmethods, any way you can't override them, so it doesn't make difference to have them in non-finalclass also.

如果它只是实用程序类,但您不希望其他类将其子类化,那么我会使用finalclass。如果实用程序类只有static方法,则无论如何您都无法覆盖它们,因此将它们放在non-final类中也没有什么区别。

回答by Jawad Zeb

Best approach for creating Utility classes. If you don't want other classes to inherit it.

创建实用程序类的最佳方法。如果您不希望其他类继承它。

//final, because it's not supposed to be subclassed
public final class AlertUtils 
{ 

// private constructor to avoid unnecessary instantiation of the class
    private AlertUtils() {
    }

  public static ..(){}//other methods
}

回答by Jiri Kremser

finalhere makes better sense than abstract. By marking the class as final, you forbid the extending the class. On the other hand marking the class as abstractis kind of opposite, because abstract class without subclasses doesn't make much sense. So it is expected to be extended.

final这里比abstract. 通过将类标记为final,您禁止扩展类。另一方面,将类标记abstract为相反,因为没有子类的抽象类没有多大意义。所以预计会延长。

回答by jt-gilkeson

Make the class final and add a private constructor. (This is what classes like java.lang.Mathuse)

使类最终并添加一个私有构造函数。(这就是类喜欢java.lang.Math使用的)

public final class A { 
    private A() {}

    public static void method() {
    }
}

回答by PermGenError

if you want other classes to use the functionality of this class then make it abstractelse make it Final

如果您希望其他类使用此类的功能,则将其设为抽象,否则将其设为Final

回答by sarathchandra-0507

These are some guidelines I've found:

这些是我发现的一些指导方针:

  • All methods must be public static, so that they cannot be overridden.
  • Constructor must be private, so it'll prevent instantiation.
  • Final keyword for the class prevents sub-classing.
  • Class should not have any non-final or non-static class fields.
  • 所有方法都必须是公共静态的,这样它们就不能被覆盖。
  • 构造函数必须是私有的,所以它会阻止实例化。
  • 类的 final 关键字可防止子类化。
  • 类不应有任何非 final 或非静态的类字段。

As you've asked, the class name cannot be abstract (not advisable) -> Which means you are planning to implement it in another class. If you want to prevent sub-classing, use final for the class name; If you want to prevent instantiation, use a private constructor.

正如您所问,类名不能是抽象的(不可取)-> 这意味着您计划在另一个类中实现它。如果要防止子类化,请使用 final 作为类名;如果要防止实例化,请使用私有构造函数。