具有公共静态的所有方法的实用程序类的正确方法是什么.
我应该使用最终类还是抽象类?
请给出建议.
例如:
public final class A{
public static void method(){
/* ... */
}
}
Run Code Online (Sandbox Code Playgroud)
要么
public abstract class A{
public static void method(){
/* ... */
}
}
Run Code Online (Sandbox Code Playgroud)
abstract
有自己的目的.如果你想要其他类(override
)实现的一些类功能,那么你使用abstract.
如果它只是实用程序类,但你不希望其他类继承它,那么我会去final
上课.如果实用程序类只有static
方法,那么任何方法都不能覆盖它们,所以在non-final
类中也没有区别.
创建实用程序类的最佳方法.如果您不希望其他类继承它.
//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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9602 次 |
最近记录: |