为什么静态方法在Java中不能是抽象的

本文翻译自: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,奥拉夫。

Java的方法分为实例方法、静态方法抽象方法和默认方法,它们有一些区别。 1. 实例方法(Instance Methods): - 实例方法是在对象上调用的方法,需要通过对象实例来调用。 - 它可以访问和修改实例变量,并且可以调用其他实例方法。 - 实例方法在每个对象实例上都有自己的一份拷贝。 2. 静态方法Static Methods): - 静态方法是属于类而不是对象的方法,可以通过类名直接调用,无需创建对象实例。 - 它不能访问实例变量,只能访问静态变量。 - 静态方法不能使用 this 关键字,也不能调用非静态方法,因为它们与具体的对象实例无关。 3. 抽象方法(Abstract Methods): - 抽象方法没有方法体,只有方法声明,使用 abstract 关键字修饰。 - 它必须在抽象类或接口声明不能在普通类声明。 - 抽象方法只有方法签名,没有具体的实现,需要在子类进行覆写实现。 4. 默认方法(Default Methods): - 默认方法是接口的一种特殊方法,使用 default 关键字修饰。 - 它提供了接口的默认实现,可以在接口直接定义方法的具体实现。 - 默认方法可以被接口的实现类直接继承和使用,也可以被子接口覆写。 总结: - 实例方法是对象级别的方法,静态方法是类级别的方法。 - 抽象方法没有具体实现,需要在子类实现,而默认方法提供了接口的默认实现。 - 实例方法和静态方法都可以有方法体,而抽象方法和默认方法可以没有具体实现。 - 实例方法可以访问和修改实例变量,而静态方法只能访问静态变量。 - 静态方法不能使用 this 关键字,也不能调用非静态方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值