繁体   English   中英

可以在子类中重写超类中的私有方法吗?

[英]Can a private method in super class be overridden in the sub-class?

可以在Java中覆盖私有方法吗? 如果不是,那么以下代码如何工作?

class Base{
      private void func(){
            System.out.println("In Base Class func method !!");         
      };
}

class Derived extends Base{
      public void func(){   //  Is this a Method Overriding..????        
            System.out.println("In Derived Class func method"); 
      }      
}

class InheritDemo{
      public static void main(String [] args){                      
            Derived d = new Derived();
            d.func(); 
      }
}

不,你没有压倒它。 您可以尝试使用@Override标记它,或尝试调用super.func(); 两者都行不通; 他们抛出编译错误。

此外,检查出来:

class Base {
      private void func(){
            System.out.println("In base func method");         
      };
      public void func2() {
          System.out.println("func2");
          func();
      }
}

class Derived extends Base {
      public void func(){   //  Is this an overriding method?
            System.out.println("In Derived Class func method"); 
      }
}

class InheritDemo {
      public static void main(String [] args) {
            Derived D = new Derived();
            D.func2(); 
      }
}

它将打印:

func2
In base func method

当您更改func()Base对公众, 那么这将是一个覆盖,输出将变为:

func2
In Derived Class func method

不,不能覆盖私有方法,因为子类不继承其父级的私有成员。 您已为子类声明了一个与超类方法无关的新方法。 查看它的一种方法是问自己在Derived类中编写super.func()是否合法。 没有办法禁止重写方法访问它覆盖的方法,但这恰恰就是这种情况。

不它不是。 你可以标记一个覆盖只是为了确保这样:

@Override
public void func(){
     System.out.println("In Derived Class func method"); 
}

在这种情况下,它将是编译器错误。

你不是压倒一切。 您不能覆盖私有成员,您只是在Derived中定义一个新方法。 Derived没有知道Base的func()实现,因为它声明为私有。 在Derived中定义func()时不会出现编译器错误,但这是因为Derived不知道Base有func()的实现。 要明确:说你覆盖Base的func()实现是不正确的。

除了已经正确的答案,请考虑以下事项:

public class Private {
    static class A {
        public void doStuff() {
            System.out.println(getStuff());
        }

        private String getStuff() {
            return "A";
        }
    }

    static class B extends A {
        public String getStuff() {
            return "B";
        }
    }

    public static void main(String[] args) {
        A a = new A();
        a.doStuff();
        a = new B();
        a.doStuff();
        B b = new B();
        b.doStuff();
    }
}

这将打印

一个

一个

一个

虽然B “覆盖”了getStuff() A中的实现doStuff()被固定在调用A#getStuff()没有多态性将被触发。

不,因为你做了像Base b = new Derived(); 你仍然无法调用b.func()。 你正在做的事情被称为“隐藏”。

由于该方法是私有的,因此其他类不可见。因此派生类不会继承此方法。 所以这不是压倒一切的情况

方法隐藏将在这里发生而不是覆盖。 就像静电一样。

实际上,你并不是压倒一切。在Java5之前

重写方法的返回类型必须与父类的方法匹配。

但Java 5引入了一个名为covariant返回类型的新工具。您可以使用相同的签名覆盖方法,但返回返回的对象的子类。 换句话说,子类中的方法可以返回一个对象,该对象的类型是由超类中具有相同签名的方法返回的类型的子类。 你可以遵循这个主题: 可以覆盖的方法在返回类型上有所不同吗?

私有方法永远不会被过度使用。 它总是隐藏的。

在您的示例中 - Derived类具有一个父类私有方法,并且具有自己的函数func 两者都不同, 功能也不会过度。 它是一个独立的独立功能。

如果在父类中调用父类函数创建一个新函数,则将调用父函数,如果使用父类引用,则在方法超出的情况下使用父函数

注意 :对象定义它拥有的成员,引用定义它可以访问的成员

//方法超越案例

class Base{
      public void func(){
            System.out.println("Parent class");         
      };                                                                   
      public void func1(){                                                
            func();                                                             
       }                                                                        
 }

class Derived extends Base{
      public void func(){         
            System.out.println("Derived class"); 
      }      
}

class InheritDemo{
      public static void main(String [] args){                      
            Derived d = new Derived();                               
            d.func1(); // Prints Derived class                                          

            Base b = new Derived();                                    
            b.func1();                                                         // Prints Derived class - no matter parent reference is calling,as there as method is overridden - Check func1() is in parent class, but id doesn't call parent class func() as the compiler finds a func() method over ridden in derived class    

      }
}

// Method Hidding case - Private and static methods case  

class Base{
      private void func(){
            System.out.println("Parent class");         
      };                                                                   
      public void func1(){                                                
            func()                                                              
      }     
}

class Derived extends Base{
      public void func(){   //  Is this a Method Overriding..????        
            System.out.println("Derived class"); 
      }      
}

class InheritDemo{
      public static void main(String [] args){                      
            Derived d = new Derived(); 
            d.func1(); // Prints Derived class
            Base b = new Derived();
            b.func1(); 
// Prints Parent class - the reason is we are using the parent class reference, so compiler is looking for func() and it founds that there is one private class method which is available and is not over ridden, so it will call it. Caution - this won't happen if called using derived class reference.

            b.func();
// this prints the Derived class - the compiler is looking func(), as Derived class has only one func() that it is implementing, so it will call that function. 

      }
}

基类的私有成员不能被类外的任何人访问,也不能被覆盖。 derive类中的函数是一个独立的函数,可以在任何地方访问。

代码将在派生类中运行该函数

阅读以下代码段中的注释以查找答案。

资料来源:

  1. 定义参考:

  2. 来自“Jeanne Boyarsky”和“Scott Selikoff”的书中的“OCA Oracle认证助理Java SE 8程序员学习指南考试1Z0-808书籍”中的源代码示例(参考)的学分。

      public class Deer { public Deer() { System.out.print("Deer"); } public Deer(int age) { System.out.print("DeerAge"); } private boolean hasHorns() { return false; } public static void main(String[] args) { Deer deer = new Reindeer(5); System.out.println(","+deer.hasHorns());// false is printed. } } class Reindeer extends Deer { public Reindeer(int age) { System.out.print("Reindeer"); } private boolean hasHorns() { return true; } // Overriding possible, but is of no use in the below context. // Below code is added by me for illustration purpose public static void main(String[] args) { Deer deer = new Reindeer(5); //Below line gives compilation error. //System.out.println(","+deer.hasHorns()); } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM