我们可以在抽象类中使用静态方法吗?

时间:2022-04-20 17:04:59

In Java Programming, Can we call a static method of an abstract class?
Yes I know we can't use static with a method of an abstract class. but I want to know why.. ?

在Java编程中,我们可以调用抽象类的静态方法吗?是的我知道我们不能使用静态方法来抽象类。但我想知道为什么..?

4 个解决方案

#1


43  

In Java you can have a static method in an abstract class:

在Java中,您可以在抽象类中使用静态方法:

abstract class Foo {
   static void bar() { }
}

This is allowed because that method can be called directly, even if you do not have an instance of the abstract class:

这是允许的,因为即使您没有抽象类的实例,也可以直接调用该方法:

Foo.bar();

However, for the same reason, you can't declare a static method to be abstract. Normally, the compiler can guarantee that an abstract method will have a real implementation any time that it is called, because you can't create an instance of an abstract class. But since a static method can be called directly, making it abstract would make it possible to call an undefined method.

但是,出于同样的原因,您不能将静态方法声明为抽象。通常,编译器可以保证抽象方法在调用它时会有实际的实现,因为您无法创建抽象类的实例。但是由于可以直接调用静态方法,因此将其抽象化可以调用未定义的方法。

abstract class Foo {
   abstract static void bar();
}

// Calling a method with no body!
Foo.bar();

In an interface, all methods are implicitly abstract. This is why an interface cannot declare a static method. (There's no architectural reason why an interface couldn't have a static method, but I suspect the writers of the JLS felt that that would encourage misuse of interfaces)

在接口中,所有方法都是隐式抽象的。这就是接口无法声明静态方法的原因。 (没有架构原因,为什么接口不能有静态方法,但我怀疑JLS的编写者认为这会鼓励滥用接口)

#2


19  

If you are talking about java, answer is Yes But you need to define the static method. You cannot create an abstract static method. What you can create is non abstract static method.

如果你在谈论java,答案是肯定但是你需要定义静态方法。您无法创建抽象静态方法。你可以创建的是非抽象静态方法。

Reason is you do not need a object instance to access a static method, so you need the method to be defined with a certain functionality.

原因是您不需要对象实例来访问静态方法,因此您需要使用某个功能定义该方法。

so you cannot have,

所以你不能拥有,

  abstract class AbstractClassExample{
     abstract static void method();


}  

But you can have,

但是你可以拥有,

abstract class AbstractClassExample{

     static void method(){}
}  

Hope this helps...

希望这可以帮助...

#3


3  

Here is a simple explanation.Abstract methods must be implemented later.We know that static methods cannot be overridden because static methods do not belong to any particular instance, rather it belongs to the class.Then different implementation of abstract method,which is static, in different classes is counter-intuitive.

这里有一个简单的解释。抽象方法必须在以后实现。我们知道静态方法不能被覆盖,因为静态方法不属于任何特定的实例,而是属于类。然后抽象方法的不同实现,这是静态的,在不同的班级是违反直觉的。

#4


3  

Yes, of course you can define the static method in abstract class. you can call that static method by using abstract class,or by using child class who extends the abstract class.Also you can able to call static method through child class instance/object. To illustrate further test following example.

是的,当然你可以在抽象类中定义静态方法。您可以通过使用抽象类或使用扩展抽象类的子类来调用该静态方法。此外,您还可以通过子类实例/对象调用静态方法。为了说明进一步测试以下示例。

//Parent class
public abstract class TestAbstractClass {
    static void testStaticMethod(){
        System.out.println("In Parent class static method");
    }
}

//child class
public class ChildClass extends TestAbstractClass {
    public static void main(String[] args) {
        TestAbstractClass parentObj = new ChildClass();
        parentObj .testStaticMethod();
        ChildClass childObj = new ChildClass();
        childObj.testStaticMethod();
        TestAbstractClass.testStaticMethod();
        childClass.testStaticMethod();
    }
}

#1


43  

In Java you can have a static method in an abstract class:

在Java中,您可以在抽象类中使用静态方法:

abstract class Foo {
   static void bar() { }
}

This is allowed because that method can be called directly, even if you do not have an instance of the abstract class:

这是允许的,因为即使您没有抽象类的实例,也可以直接调用该方法:

Foo.bar();

However, for the same reason, you can't declare a static method to be abstract. Normally, the compiler can guarantee that an abstract method will have a real implementation any time that it is called, because you can't create an instance of an abstract class. But since a static method can be called directly, making it abstract would make it possible to call an undefined method.

但是,出于同样的原因,您不能将静态方法声明为抽象。通常,编译器可以保证抽象方法在调用它时会有实际的实现,因为您无法创建抽象类的实例。但是由于可以直接调用静态方法,因此将其抽象化可以调用未定义的方法。

abstract class Foo {
   abstract static void bar();
}

// Calling a method with no body!
Foo.bar();

In an interface, all methods are implicitly abstract. This is why an interface cannot declare a static method. (There's no architectural reason why an interface couldn't have a static method, but I suspect the writers of the JLS felt that that would encourage misuse of interfaces)

在接口中,所有方法都是隐式抽象的。这就是接口无法声明静态方法的原因。 (没有架构原因,为什么接口不能有静态方法,但我怀疑JLS的编写者认为这会鼓励滥用接口)

#2


19  

If you are talking about java, answer is Yes But you need to define the static method. You cannot create an abstract static method. What you can create is non abstract static method.

如果你在谈论java,答案是肯定但是你需要定义静态方法。您无法创建抽象静态方法。你可以创建的是非抽象静态方法。

Reason is you do not need a object instance to access a static method, so you need the method to be defined with a certain functionality.

原因是您不需要对象实例来访问静态方法,因此您需要使用某个功能定义该方法。

so you cannot have,

所以你不能拥有,

  abstract class AbstractClassExample{
     abstract static void method();


}  

But you can have,

但是你可以拥有,

abstract class AbstractClassExample{

     static void method(){}
}  

Hope this helps...

希望这可以帮助...

#3


3  

Here is a simple explanation.Abstract methods must be implemented later.We know that static methods cannot be overridden because static methods do not belong to any particular instance, rather it belongs to the class.Then different implementation of abstract method,which is static, in different classes is counter-intuitive.

这里有一个简单的解释。抽象方法必须在以后实现。我们知道静态方法不能被覆盖,因为静态方法不属于任何特定的实例,而是属于类。然后抽象方法的不同实现,这是静态的,在不同的班级是违反直觉的。

#4


3  

Yes, of course you can define the static method in abstract class. you can call that static method by using abstract class,or by using child class who extends the abstract class.Also you can able to call static method through child class instance/object. To illustrate further test following example.

是的,当然你可以在抽象类中定义静态方法。您可以通过使用抽象类或使用扩展抽象类的子类来调用该静态方法。此外,您还可以通过子类实例/对象调用静态方法。为了说明进一步测试以下示例。

//Parent class
public abstract class TestAbstractClass {
    static void testStaticMethod(){
        System.out.println("In Parent class static method");
    }
}

//child class
public class ChildClass extends TestAbstractClass {
    public static void main(String[] args) {
        TestAbstractClass parentObj = new ChildClass();
        parentObj .testStaticMethod();
        ChildClass childObj = new ChildClass();
        childObj.testStaticMethod();
        TestAbstractClass.testStaticMethod();
        childClass.testStaticMethod();
    }
}