PC.*_*PC. 5 c# java static abstract-class final
假设我有一个只包含静态方法和变量的实用程序类.例如:
public abstract final class StringUtils
{
public static final String NEW_LINE = System.getProperty("line.separator");
public static boolean isNotNullOrSpace(final String string)
{
return !(string == null || string.length() < 1 || string.trim().length() < 1);
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,使类既抽象又最终是有意义的.摘要因为制作这个类的对象没有用,因为所有方法都可以静态访问.最后因为派生类不能从该类继承任何内容,因为它没有任何非静态成员.
C#允许这些类的静态修饰符.为什么Java不支持这个?
ssa*_*tos 11
甲final类不能扩展,一个abstract类需要被扩展在被实例化的顺序.因此,一个final abstract阶级将是一个逻辑矛盾.
如果你的类只有static方法,也许你应该只是hide它的构造函数,通过将它定义为private.-
private StringUtils() {
}
Run Code Online (Sandbox Code Playgroud)