简体   繁体   English

继承和方法

[英]Inheritance and methods

I'm having a problem with the constructors of classes and sub-classes(that extend the first class) 我的类和子类的构造函数有问题(扩展了第一个类)

This is the basic class's constructor: 这是基本类的构造函数:

    public Airplane(int x, int y, int width)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        createSeats();
    }

And here is another class's constructor that extends the basic class 这是扩展基本类的另一个类的构造函数

public Boeing(int x, int y, int width)
{
    super(x,y,width);
    createSeats();
}

The problem is that createSeats() is another method in both classes, but Boeing has to override the main one. 问题在于createSeats()是这两个类中的另一个方法,但Boeing必须重写主要方法。 How can I make it so that this will work without taking the createSeats() method out? 我如何才能做到这一点而又无需取出createSeats()方法呢?

If you know you want all subclasses of Airplane to call createSeats() on construction then you can leave the call in the Airplane constructor and remove from Boeing . 如果您知道要让Airplane所有子类在构造时调用createSeats() ,则可以将该调用保留在Airplane构造函数中,并从Boeing删除。

If you only want particular subclasses, eg Boeing to call createSeats() on construction then you can leave the call in the Boeing constructor and remove from Airplane . 如果只希望特定的子类,例如Boeing在构造时调用createSeats() ,则可以将该调用保留在Boeing构造函数中,然后从Airplane删除。

You don't need the invocations in both constructors, leaving them there will result in createSeats() being called twice for each Boeing you initialise. 不需要这两个构造函数中的调用,将它们保留在那里将导致createSeats()对于您初始化的每个Boeing都被调用两次。

The call to the super constructor already includes the call to the createSeats() method, so you don't have to reinclude it in the Boeing constructor. 对超级构造函数的调用已经包括对createSeats()方法的调用,因此您不必将其重新包含在Boeing构造函数中。

If you only want to change the behavior of creating seats, just override that method. 如果您只想更改创建座位的行为,只需重写该方法即可。

class Airplane {
  public Airplane(int x, int y, int width)
  {
    this.x = x;
    this.y = y;
    this.width = width;
    createSeats();
  }

  void createSeats() { /* create them */ }
}
class Boeing {
  public Boeing(int x, int y, int width)
  {
    super(x,y,width); // same steps as super class
  }

  @Override
  void createSeats()
  {
    // do something else entirely
    // possibly call super.createSeats()
  }
}

Frequently a default implementation of a method is provided in a super class, which allows you to customize the behavior in a subclass by overriding the method in a subclass. 通常,在超类中提供方法的默认实现,该方法使您可以通过重写子类中的方法来自定义子类中的行为。 Eg Does an Airplane know how to create seats on a Boeing ? 例如, 飞机是否知道如何在波音 飞机上制造座位? Probably not, therefore you would override the createSeats () in your subclass to provide the specific implementation that works for you particular type of Airplane . 可能不是,因此您将重写子类中的createSeats ()以提供适用于您特定类型的Airplane的特定实现。

The fact that the createSeats () method is called from inside your super class' constructor guarantees that the method in question will always be called on construction of the object. 从您的超类的构造函数内部调用createSeats ()方法的事实保证了始终在对象的构造上调用该方法。 This is a common design pattern. 这是一种常见的设计模式。 It's called template method. 这称为模板方法。 Basically you have a method that encapsulates the execution of a number of methods in a certain order to produce an outcome. 基本上,您有一个方法以某种顺序封装了许多方法的执行以产生结果。 In your case there's one method namely createSeats (). 在您的情况下,有一种方法称为createSeats ()。

I agree with @milkplusvellocet's answer, however you also said "The problem is that createSeats() is another method in both classes, but Boeing has to override the main one." 我同意@milkplusvellocet的回答,但是您还说过“问题是createSeats()是这两个类中的另一种方法,但波音必须重写主要方法。” If you ever need to call a superclass's method instead of your own, you can always call super.createSeats() . 如果您需要调用超类的方法而不是自己的方法,则可以始终调用super.createSeats() That way you can always differentiate between methods in the superclass and your own methods. 这样,您始终可以区分超类中的方法和您自己的方法。

However, that said, that's almost always used in the method you're overriding. 但是,那几乎总是在您要覆盖的方法中使用。 For example, 例如,

@Override
public void createSeats() {
  doSomethingSpecial();
  super.createSeats();
}

@Override @Override

before the method you want to override should do the job 在您要覆盖的方法应做的工作之前

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

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