问题是,在Java中为什么不能定义抽象静态方法?例如

abstract class foo {
    abstract void bar( ); // <-- this is ok
    abstract static void bar2(); //<-- this isn't why?
}

当前回答

我也问了同样的问题,原因如下

因为抽象类说,它不会给出实现,并允许子类给出它

所以子类必须重写超类的方法,

规则1 -静态方法不能被覆盖

因为静态成员和方法是编译时元素,这就是为什么允许重载(编译时多态性)静态方法而不是重写(运行时多态性)

所以它们不可能是抽象的。

Java宇宙中不允许有抽象静态<——之类的东西

其他回答

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.

你不能重写静态方法,所以使它抽象是没有意义的。此外,抽象类中的静态方法将属于该类,而不是覆盖类,因此无论如何都不能使用。

首先,关于抽象类的一个关键点—— 抽象类不能被实例化(参见wiki)。因此,您不能创建抽象类的任何实例。

现在,java处理静态方法的方法是与该类的所有实例共享该方法。

所以,如果你不能实例化一个类,这个类就不能有抽象静态方法,因为抽象方法需要扩展。

繁荣。

因为抽象类是一个OOPS概念,静态成员不是OOPS....的一部分 现在我们可以在接口中声明静态完整方法,我们可以通过在接口中声明主方法来执行接口

interface Demo 
{
  public static void main(String [] args) {
     System.out.println("I am from interface");
  }
}

使用抽象静态方法的想法是,您不能直接为该方法使用特定的抽象类,但只允许一阶导数实现该静态方法(或者对于泛型:您使用的泛型的实际类)。

通过这种方式,您可以创建sortableObject抽象类甚至接口 使用(auto-)抽象静态方法,定义排序选项的参数:

public interface SortableObject {
    public [abstract] static String [] getSortableTypes();
    public String getSortableValueByType(String type);
}

现在你可以定义一个可排序对象,它可以根据所有这些对象的主要类型进行排序:

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
    }
}

现在您可以创建一个

public class SortableList<T extends SortableObject> 

它可以检索类型,构建一个弹出菜单来选择要排序的类型,并通过从该类型获取数据来返回列表,以及hainv一个add函数,当选择了排序类型时,可以自动对新项进行排序。 注意SortableList实例可以直接访问“T”的静态方法:

String [] MenuItems = T.getSortableTypes();

必须使用实例的问题是SortableList可能还没有项目,但已经需要提供首选排序。

再见 奥拉夫。