【问题标题】:Polymorphism instead of over-riding method, adding on多态性而不是覆盖方法,添加
【发布时间】:2017-09-08 04:30:33
【问题描述】:

我想知道。当我有两个类而第二个类metalRobot 扩展了第一个类robot 时,有没有办法在第二个方法中调用第一个类的方法来覆盖它?

示例

//super class
class robot{
   private int width;
   private int height;

   //constructor
   robot(int width,int height){
    this.width=height;
    this.width=height;
   }

   //display width and height
   void render()
   {
     System.out.println(width + " " + height);
   }

}


//child class
class metalRobot{
   private int x;
   private int y;

   //constructor
   metalRobot(int x,int y, int height, int width){
    super(width,height);
    this.x=x;
    this.y=y;
   }

   @Override
   void render()
   {
     System.out.println(x + " " + y);
   }

}

所以当我调用渲染时,它会打印出 x,y 宽度和高度

【问题讨论】:

  • 您的metalRobot 需要扩展robot 类,否则它不是子类。
  • Java 中的类名,按照惯例,应该使用大写。
  • 在教学时,我告诉我的学生,当我看到一个不包含 super 调用的覆盖方法时,我通常会怀疑,例如 super.render();

标签: java polymorphism overriding


【解决方案1】:

在metalRobot中render()的第一行调用super.render()。

其他信息:https://docs.oracle.com/javase/tutorial/java/IandI/super.html

【讨论】:

    【解决方案2】:

    首先你需要从robot继承:

    class metalRobot extends robot {
    }
    

    然后就可以通过关键字super访问父方法为:

    @Override
    void render() {
        super.render();
        System.out.println(width + " " + height);
    }
    

    【讨论】:

      【解决方案3】:

      致电super.render() 是一种解决方案。另一种我个人更喜欢的解决方案是使用the decorator pattern。它需要一个额外的接口,但它使您的类的用户可以更好地控制他们如何组成一个复合对象。

      interface Robot
      {
          void render();
      }
      
      class DefaultRobot implements Robot
      {
          public void render()
          {
              //do some stuff
          }
      }
      
      class MetalRobot implements Robot
      {
          private final Robot wrapped;
      
          public MetalRobot(Robot robot)
          {
              this.wrapped = robot;
          }
      
          public void render()
          {
              wrapped.render();
              // do some extra stuff
          }
      }
      

      示例用法可能类似于:

      Robot robot = new MetalRobot(new DefaultRobot());
      

      假设您稍后要添加FatRobotRedRobot,您可以这样做:

      // metal, fat, red robot
      Robot robot = new MetalRobot(new FatRobot(new RedRobot(new DefaultRobot())));
      
      // fat and red robot
      Robot robot2 = new FatRobot(new RedRobot(new DefaultRobot()));
      
      // red, metal robot
      Robot robot3 = new RedRobot(new MetalRobot(new DefaultRobot()));
      

      或您能想象的任何其他组合。多态性不允许让您以优雅的方式做到这一点。

      【讨论】:

        【解决方案4】:

        使用super.render()访问父类的方法

        【讨论】:

          【解决方案5】:

          为了调用超类的重写方法,您必须使用super

          关键字后跟一个点,然后是方法名称。

          例如super.render()

          注意super 只允许在子类的非静态方法中使用。

          当你有一个构造函数时,你也可以在你的构造函数中使用super

          不是默认构造函数的父类,您必须提供所需的

          通过子类构造函数在超类构造函数中的参数

          例如如果你的超类有MySuperclass(String myString)的构造函数

          并且不包含默认构造函数,

          您必须为超类构造函数提供值才能使用super 关键字

          MySubclass()
          {
             super("Needed String");
          }
          

          还请注意,java 中不允许多重继承,因此您不能多次 extends,例如2次以上

          为什么?

          因为子类不知道哪个 super 是哪个以及许多其他原因,这就是 James Gosling 不允许这样做的原因

          【讨论】:

            猜你喜欢
            • 2011-01-11
            • 1970-01-01
            • 1970-01-01
            • 2021-06-30
            • 1970-01-01
            • 1970-01-01
            • 2018-06-21
            • 2018-12-13
            • 2013-05-04
            相关资源
            最近更新 更多