简体   繁体   English

使用超级关键字覆盖方法

[英]Method Overriding using super keyword

I have one doubt, Please see the following code. 我有一个疑问,请参见以下代码。 i have three classes A , B and InheritanceExample . 我有三个类ABInheritanceExample Here I am calling the super.run() from the main class; 在这里,我从主类调用super.run() it is calling the B class run() method. 它正在调用B类的run()方法。

Is there any option to call A class run method from the main class ( InheritanceExample ) with out creating an instance for class A? 是否可以从主类( InheritanceExample )调用A类运行方法,而InheritanceExample为A类创建实例?

class A
{
    void run()
    {
        System.out.println("<<<====Class A run Method===>>>>");
    }
}

class B extends A
{
    void run()
    {

        System.out.println("<<<====Class B run Method===>>>>");
        super.run();
    }
}

public class InheritanceExample extends B{

    /**
     * @param args
     */
    void run()
    {
        System.out.println("<<<====Main Class run Method===>>>>");
        super.run();
    }
public static void main(String[] args) {
    InheritanceExample a = new InheritanceExample();
    a.run();
} 
    }

Since B extends A and InheritanceExample extends B you are creating an instance. 由于B 扩展了 AInheritanceExample 扩展了 B您正在创建一个实例。 Make method A.run() static . 使方法A.run() 静态

class A
{
    void static run()
    {
        System.out.println("<<<====Class A run Method===>>>>");
    }
}

class B
{
    void run()
    {
        System.out.println("<<<====Class B run Method===>>>>");
        A.run();
    }
}

public class InheritanceExample extends B {

    @Override
    void run()
    {
        System.out.println("<<<====Main Class run Method===>>>>");
        super.run();
    }

    public static void main(String[] args) {
        InheritanceExample a = new InheritanceExample();
        a.run();
    } 
}

No. 没有。

Not without making A.run() different from B.run(), such as by making A.run() static. 并非不使A.run()与B.run()不同,例如使A.run()静态化。

When B extends A, you as a programmer must ensure that from the callers perspective B "is-a" A. What you want to do is to break this rule. 当B扩展A时,作为程序员,您必须确保从调用者的角度来看B“是-A”A。您要做的是打破此规则。

If you want to use a B as an A, you are probably trying to do something at the calling point, that should be handled internally in B. 如果要将B用作A,则可能正在尝试在调用点执行某些操作,这些操作应在B内部进行处理。

Not sure what you want to achieve. 不确定要实现什么。 But looking at the class hierarchy and structure, it is not possible to directly call run method of Class A. But if we change and introduce a additional static method say runImpl in Class A, and call same method from run method of Class A. Now we can call runImpl from anywhere as it is static and run method too internally is calling runImpl so same implementation is getting call via run and runImpl. 但是,从类的层次结构和结构来看,不可能直接调用A类的run方法。但是,如果我们更改并引入了另一个静态方法,请在Class A中使用runImpl,然后从Class A的run方法中调用相同的方法。我们可以从任何地方调用runImpl,因为它是静态的,而且run方法在内部也正在调用runImpl,因此同一实现通过run和runImpl获得调用。

Below is the code snippet: 下面是代码片段:

class A
{
    void run()
    {   
        runImpl();
    }

    public static void runImpl(){
        System.out.println("<<<====Class A run Method===>>>>");
    }
}

class B extends A
{
    void run()
    {

        System.out.println("<<<====Class B run Method===>>>>");
        super.run();
    }   

}

public class InheritanceExample extends B{

    /**
     * @param args
     */
    void run()
    {
        System.out.println("<<<====Main Class run Method===>>>>");
        super.run();
    }
    public static void main(String[] args) {
        A.runImpl();
    } 
}
  class a{

      void run(){

          System.out.println("runing method run of a clss");

      }
  }
  class b extends a{
      void run(){
          super.run();
          System.out.println("runing method run of b clss");
      }

  }
  class InheritanceExample extends b{
      void run(){
          super.run();
          System.out.println("running method run in inheritance class");
      }
  }

  class pratics{
      public static void main(String[] args){
        System.out.println("********");
        InheritanceExample a1=new InheritanceExample();
        a1.run();
       System.out.println("********");
      }
  }

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

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