实用程序类 - 什么是正确的方法? [英] Utility Class - What is the correct approach?
问题描述
对于所有具有公共静态的方法的实用程序类,正确的方法是什么。
我应该使用final类还是抽象类?
请给出建议。
例如:
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(){
}
}
或
public abtstract class A{
public static void method(){
}
}
推荐答案
abstract
如果你想要一些由其他类( override
)实现的类功能,那么你使用abstract。
abstract
has its own purpose. If you want some of the class functionality implemented by other classes (override
) then you use abstract.
只是实用程序类,但你不想要其他类的子类,然后我会用 final
类。如果实用程序类只有 static
方法,任何方式你不能覆盖它们,因此它们没有区别,它们在非final
class also。
If it is just utility class, but you don't want other classes subclass it, then I would go with final
class. If utility class has just static
methods, any way you can't override them, so it doesn't make difference to have them in non-final
class also.
这篇关于实用程序类 - 什么是正确的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!