本文翻译自:Why can't static methods be abstract in Java
The question is in Java why can't I define an abstract static method? 问题是在Java中为什么我不能定义一个抽象的静态方法? for example 例如
abstract class foo {
abstract void bar( ); // <-- this is ok
abstract static void bar2(); //<-- this isn't why?
}
#1楼
参考:https://stackoom.com/question/1YVG/为什么静态方法在Java中不能是抽象的
#2楼
An abstract method is defined only so that it can be overridden in a subclass. 仅定义抽象方法,以便可以在子类中重写它。 However, static methods can not be overridden. 但是,无法覆盖静态方法。 Therefore, it is a compile-time error to have an abstract, static method. 因此,具有抽象的静态方法是编译时错误。
Now the next question is why static methods can not be overridden?? 现在接下来的问题是为什么静态方法不能被覆盖?
It's because static methods belongs to a particular class and not to its instance. 这是因为静态方法属于特定类而不属于其实例。 If you try to override a static method you will not get any compilation or runtime error but compiler would just hide the static method of superclass. 如果您尝试覆盖静态方法,则不会得到任何编译或运行时错误,但编译器只会隐藏超类的静态方法。
#3楼
Because if a class extends an abstract class then it has to override abstract methods and that is mandatory. 因为如果一个类扩展了一个抽象类,那么它必须覆盖抽象方法,这是必需的。 And since static methods are class methods resolved at compile time whereas overridden methods are instance methods resolved at runtime and following dynamic polymorphism. 并且因为静态方法是在编译时解析的类方法,而重写的方法是在运行时和动态多态性之后解析的实例方法。
#4楼
Poor language design. 语言设计不佳。 It would be much more effective to call directly a static abstract method than creating an instance just for using that abstract method. 直接调用静态抽象方法比仅使用该抽象方法创建实例会更有效。 Especially true when using an abstract class as a workaround for enum inability to extend, which is another poor design example. 当使用抽象类作为枚举无法扩展的变通方法时尤其如此,这是另一个糟糕的设计示例。 Hope they solve those limitations in a next release. 希望他们在下一个版本中解决这些限制。
#5楼
I also asked the same question , here is why 我也问了同样的问题,这就是原因
Since Abstract class says, it will not give implementation and allow subclass to give it 从Abstract类开始说,它不会给出实现并允许子类给它
so Subclass has to override the methods of Superclass , 所以Subclass必须覆盖Superclass的方法,
RULE NO 1 - A static method cannot be overridden RULE NO 1 - 无法覆盖静态方法
Because static members and methods are compile time elements , that is why Overloading(Compile time Polymorphism) of static methods are allowed rather then Overriding (Runtime Polymorphism) 因为静态成员和方法是编译时元素,这就是为什么允许静态方法的重载(编译时多态)而不是覆盖(运行时多态)
So , they cant be Abstract . 所以,他们不能抽象。
There is no thing like abstract static <--- Not allowed in Java Universe 没有抽象静态 <--- Java Universe中不允许的东西
#6楼
The idea of having an abstract static method would be that you can't use that particular abstract class directly for that method, but only the first derivative would be allowed to implement that static method (or for generics: the actual class of the generic you use). 有一个抽象静态方法的想法是你不能直接为该方法使用那个特定的抽象类,但只允许一阶导数实现那个静态方法(或者对于泛型:通用的实际类你使用)。
That way, you could create for example a sortableObject abstract class or even interface with (auto-)abstract static methods, which defines the parameters of sort options: 这样,您可以创建一个sortableObject抽象类,甚至可以使用(自动)抽象静态方法创建接口,这些方法定义了排序选项的参数:
public interface SortableObject {
public [abstract] static String [] getSortableTypes();
public String getSortableValueByType(String type);
}
Now you can define a sortable object that can be sorted by the main types which are the same for all these objects: 现在,您可以定义一个可排序对象,该对象可以按所有这些对象相同的主要类型进行排序:
public class MyDataObject implements SortableObject {
final static String [] SORT_TYPES = {
"Name","Date of Birth"
}
static long newDataIndex = 0L ;
String fullName ;
String sortableDate ;
long dataIndex = -1L ;
public MyDataObject(String name, int year, int month, int day) {
if(name == null || name.length() == 0) throw new IllegalArgumentException("Null/empty name not allowed.");
if(!validateDate(year,month,day)) throw new IllegalArgumentException("Date parameters do not compose a legal date.");
this.fullName = name ;
this.sortableDate = MyUtils.createSortableDate(year,month,day);
this.dataIndex = MyDataObject.newDataIndex++ ;
}
public String toString() {
return ""+this.dataIndex+". "this.fullName+" ("+this.sortableDate+")";
}
// override SortableObject
public static String [] getSortableTypes() { return SORT_TYPES ; }
public String getSortableValueByType(String type) {
int index = MyUtils.getStringArrayIndex(SORT_TYPES, type);
switch(index) {
case 0: return this.name ;
case 1: return this.sortableDate ;
}
return toString(); // in the order they were created when compared
}
}
Now you can create a 现在你可以创建一个
public class SortableList<T extends SortableObject>
that can retrieve the types, build a pop-up menu to select a type to sort on and resort the list by getting the data from that type, as well as hainv an add function that, when a sort type has been selected, can auto-sort new items in. Note that the instance of SortableList can directly access the static method of "T": 可以检索类型,构建弹出菜单以选择要排序的类型并通过从该类型获取数据来求助列表,以及hainv添加函数,当选择排序类型时,可以自动-sort new items in。注意SortableList的实例可以直接访问静态方法“T”:
String [] MenuItems = T.getSortableTypes();
The problem with having to use an instance is that the SortableList may not have items yet, but already need to provide the preferred sorting. 必须使用实例的问题是SortableList可能还没有项目,但已经需要提供首选排序。
Cheerio, Olaf. Cheerio,奥拉夫。