📜  我们可以覆盖Java中的私有方法吗?

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

我们可以覆盖Java中的私有方法吗?

让我们首先考虑以下Java程序作为覆盖或运行时多态性的简单示例。

Java
class Base {
  public void fun() {
     System.out.println("Base fun");    
  }
}
   
class Derived extends Base {
  public void fun() {  // overrides the Base's fun()
     System.out.println("Derived fun");    
  }
  public static void main(String[] args) {
      Base obj = new Derived();
      obj.fun();
  } 
}


Java
class Base {
  private void fun() {
     System.out.println("Base fun");    
  }
}
   
class Derived extends Base {
  private void fun() {
     System.out.println("Derived fun");    
  }
  public static void main(String[] args) {
      Base obj = new Derived();
      obj.fun();
  } 
}


Java
/* Java program to demonstrate whether we can override private method
   of outer class inside its inner class */
class Outer {
     private String msg = "GeeksforGeeks";
     private void fun() {
          System.out.println("Outer fun()");
     }
 
     class Inner extends Outer {
         private void fun()  {
               System.out.println("Accessing Private Member of Outer: " + msg);
         }
     }
 
     public static void main(String args[])  {
 
          // In order to create instance of Inner class, we need an Outer
          // class instance. So, first create Outer class instance and then
          // inner class instance.
          Outer o = new Outer();
          Inner  i   = o.new Inner();
           
          // This will call Inner's fun, the purpose of this call is to
          // show that private members of Outer can be accessed in Inner.
          i.fun(); 
 
          // o.fun() calls Outer's fun (No run-time polymorphism).
          o = i;
          o.fun();
     }
}


该程序打印“衍生乐趣”。
基类引用“obj”是指派生类对象(参见表达式“Base obj = new Derived()”)。当在 obj 上调用 fun() 时,调用是根据引用对象的类型进行的,而不是根据引用进行的。

私有方法可以覆盖吗?  
预测以下程序的输出。

Java

class Base {
  private void fun() {
     System.out.println("Base fun");    
  }
}
   
class Derived extends Base {
  private void fun() {
     System.out.println("Derived fun");    
  }
  public static void main(String[] args) {
      Base obj = new Derived();
      obj.fun();
  } 
}

我们得到编译器错误“fun() has private access in Base”(见这个)。所以编译器尝试调用基类函数,而不是派生类,这意味着 fun() 没有被覆盖。

内部类可以访问其外部类的私有成员。如果我们扩展一个内部类并在内部类中创建 fun() 会怎样?
Inner 类可以访问其外部类的私有成员,例如在下面的程序中, Innerfun()访问私有数据成员msg ,编译器可以这样做。

Java

/* Java program to demonstrate whether we can override private method
   of outer class inside its inner class */
class Outer {
     private String msg = "GeeksforGeeks";
     private void fun() {
          System.out.println("Outer fun()");
     }
 
     class Inner extends Outer {
         private void fun()  {
               System.out.println("Accessing Private Member of Outer: " + msg);
         }
     }
 
     public static void main(String args[])  {
 
          // In order to create instance of Inner class, we need an Outer
          // class instance. So, first create Outer class instance and then
          // inner class instance.
          Outer o = new Outer();
          Inner  i   = o.new Inner();
           
          // This will call Inner's fun, the purpose of this call is to
          // show that private members of Outer can be accessed in Inner.
          i.fun(); 
 
          // o.fun() calls Outer's fun (No run-time polymorphism).
          o = i;
          o.fun();
     }
}

输出:

Accessing Private Member of Outer: GeeksforGeeks
Outer fun()

在上面的程序中,我们创建了一个外部类和一个内部类。我们从 Outer 扩展了 Inner,并在 Outer 和 Inner 中创建了一个 fun() 方法。如果我们观察我们的输出,那么很明显 fun() 方法没有被覆盖。之所以如此,是因为私有方法在编译时是绑定的,并且是引用变量的类型——而不是它所引用的对象的类型——决定了要调用的方法。 .附带说明一下,由于静态绑定,私有方法可能在性能方面更好(与非私有和非最终方法相比)。