📜  我们可以在Java中重载或覆盖静态方法吗?

📅  最后修改于: 2022-05-13 01:54:26.965000             🧑  作者: Mango

我们可以在Java中重载或覆盖静态方法吗?

让我们首先定义重载和覆盖。
覆盖:覆盖是Java等 OOP 语言的一个特性,它与运行时多态性有关。子类(或派生类)提供超类(或基类)中方法的特定实现。
要执行的实现是在运行时决定的,并根据用于调用的对象做出决定。请注意,两种方法的签名必须相同。有关详细信息,请参阅Java中的覆盖。
重载:重载也是Java等 OOP 语言的一个特性,它与编译时(或静态)多态性有关。这个特性允许不同的方法具有相同的名称,但不同的签名,尤其是输入参数的数量和输入参数的类型。请注意,在 C++ 和Java中,不能根据返回类型重载方法。
我们可以重载静态方法吗?
答案是“是”。我们可以有两个或多个同名但输入参数不同的静态方法。例如,考虑以下Java程序。

Java
// filename Test.java
public class Test {
    public static void foo() {
        System.out.println("Test.foo() called ");
    }
    public static void foo(int a) {
        System.out.println("Test.foo(int) called ");
    }
    public static void main(String args[])
    {
        Test.foo();
        Test.foo(10);
    }
}


Java
// filename Test.java
public class Test {
    public static void foo() {
        System.out.println("Test.foo() called ");
    }
    public void foo() { // Compiler Error: cannot redefine foo()
        System.out.println("Test.foo(int) called ");
    }
    public static void main(String args[]) {
        Test.foo();
    }
}


Java
/* Java program to show that if static method is redefined by
   a derived class, then it is not overriding. */
 
// Superclass
class Base {
     
    // Static method in base class which will be hidden in subclass
    public static void display() {
        System.out.println("Static or class method from Base");
    }
     
     // Non-static method which will be overridden in derived class
     public void print()  {
         System.out.println("Non-static or Instance method from Base");
    }
}
 
// Subclass
class Derived extends Base {
     
    // This method is hidden by display() in Base
    public static void display() {
         System.out.println("Static or class method from Derived");
    }
     
    // This method overrides print() in Base
    public void print() {
         System.out.println("Non-static or Instance method from Derived");
   }
}
 
// Driver class
public class Test {
    public static void main(String args[ ])  {
       Base obj1 = new Derived();
        
       // As per overriding rules this should call to class Derive's static
       // overridden method. Since static method can not be overridden, it
       // calls Base's display()
       obj1.display(); 
        
       // Here overriding works and Derive's print() is called
       obj1.print();    
    }
}


Java
/* Java program to show that if static methods are redefined by
   a derived class, then it is not overriding but hidding. */
 
// Superclass
class Base {
     
    // Static method in base class which will be hidden in subclass
    public static void display() {
        System.out.println("Static or class method from Base");
    }
     
     // Non-static method which will be overridden in derived class
     public void print()  {
         System.out.println("Non-static or Instance method from Base");
    }
}
 
// Subclass
class Derived extends Base {
     
    // Static is removed here (Causes Compiler Error)
    public void display() {
        System.out.println("Non-static method from Derived");
    }
     
    // Static is added here (Causes Compiler Error)
    public static void print() {
        System.out.println("Static method from Derived");
   }
}


输出
Test.foo() called 
Test.foo(int) called 

我们可以重载仅通过静态关键字不同的方法吗?
如果它们仅通过静态关键字不同(参数数量和参数类型相同),我们不能在Java中重载两个方法。例如,请参见以下Java程序。这种行为在 C++ 中是相同的(参见第 2 点)。

Java

// filename Test.java
public class Test {
    public static void foo() {
        System.out.println("Test.foo() called ");
    }
    public void foo() { // Compiler Error: cannot redefine foo()
        System.out.println("Test.foo(int) called ");
    }
    public static void main(String args[]) {
        Test.foo();
    }
}

输出:

Compiler Error, cannot redefine foo()

我们可以覆盖Java中的静态方法吗?
我们可以在子类中声明具有相同签名的静态方法,但它不被视为重写,因为不会有任何运行时多态性。因此答案是“否”。
如果派生类定义了与基类中的静态方法具有相同签名的静态方法,则派生类中的方法被基类中的方法隐藏。

Java

/* Java program to show that if static method is redefined by
   a derived class, then it is not overriding. */
 
// Superclass
class Base {
     
    // Static method in base class which will be hidden in subclass
    public static void display() {
        System.out.println("Static or class method from Base");
    }
     
     // Non-static method which will be overridden in derived class
     public void print()  {
         System.out.println("Non-static or Instance method from Base");
    }
}
 
// Subclass
class Derived extends Base {
     
    // This method is hidden by display() in Base
    public static void display() {
         System.out.println("Static or class method from Derived");
    }
     
    // This method overrides print() in Base
    public void print() {
         System.out.println("Non-static or Instance method from Derived");
   }
}
 
// Driver class
public class Test {
    public static void main(String args[ ])  {
       Base obj1 = new Derived();
        
       // As per overriding rules this should call to class Derive's static
       // overridden method. Since static method can not be overridden, it
       // calls Base's display()
       obj1.display(); 
        
       // Here overriding works and Derive's print() is called
       obj1.print();    
    }
}
输出
Static or class method from Base
Non-static or Instance method from Derived

以下是Java中方法覆盖和静态方法的一些要点。
1)对于类(或静态)方法,根据引用的类型调用方法,而不是根据被引用的对象,这意味着方法调用是在编译时决定的。
2)对于实例(或非静态)方法,方法是根据被引用对象的类型调用的,而不是根据引用的类型,这意味着方法调用是在运行时决定的。
3)实例方法不能覆盖静态方法,静态方法不能隐藏实例方法。例如,以下程序有两个编译器错误。

Java

/* Java program to show that if static methods are redefined by
   a derived class, then it is not overriding but hidding. */
 
// Superclass
class Base {
     
    // Static method in base class which will be hidden in subclass
    public static void display() {
        System.out.println("Static or class method from Base");
    }
     
     // Non-static method which will be overridden in derived class
     public void print()  {
         System.out.println("Non-static or Instance method from Base");
    }
}
 
// Subclass
class Derived extends Base {
     
    // Static is removed here (Causes Compiler Error)
    public void display() {
        System.out.println("Non-static method from Derived");
    }
     
    // Static is added here (Causes Compiler Error)
    public static void print() {
        System.out.println("Static method from Derived");
   }
}

4)在子类(或派生类)中,我们可以重载继承自超类的方法。这样的重载方法既不会隐藏也不会覆盖超类方法——它们是新方法,是子类独有的。