文档

Java™ 教程-Java Tutorials 中文版
多态
Trail: Learning the Java Language
Lesson: Interfaces and Inheritance
Section: Inheritance

多态

polymorphism (多态) 的字典定义是指生物学中的原理,表示生物或物种可以具有许多不同的形式或阶段。这个原则也可以应用于面向对象的编程像 Java 语言之类的语言。类的子类可以定义它们自己的唯一行为,但仍然共享父类的一些相同功能。

可以通过对 Bicycle 类的微小修改来演示多态性。例如,可以将 printDescription 方法添加到类中,以显示当前存储在实例中的所有数据。

public void printDescription(){
    System.out.println("\nBike is " + "in gear " + this.gear
        + " with a cadence of " + this.cadence +
        " and travelling at a speed of " + this.speed + ". ");
}

要演示 Java 语言中的多态特性,请使用 MountainBikeRoadBike 类继承 Bicycle 类。对于 MountainBike 类,为 suspension 添加一个字段,这是 String 值,表示自行车是否有前减震器,Front。或者,自行车有前后减震器,Dual

这是更新后的:

public class MountainBike extends Bicycle {
    private String suspension;

    public MountainBike(
               int startCadence,
               int startSpeed,
               int startGear,
               String suspensionType){
        super(startCadence,
              startSpeed,
              startGear);
        this.setSuspension(suspensionType);
    }

    public String getSuspension(){
      return this.suspension;
    }

    public void setSuspension(String suspensionType) {
        this.suspension = suspensionType;
    }

    public void printDescription() {
        super.printDescription();
        System.out.println("The " + "MountainBike has a" +
            getSuspension() + " suspension.");
    }
} 

请注意重写的 printDescription 方法。除了之前提供的信息之外,还有关于 suspension 的其他数据包含在输出中。

接下来,创建 RoadBike 类。因为公路或赛车有紧身轮胎,所以添加一个属性来跟踪轮胎宽度。这是 RoadBike 类:

public class RoadBike extends Bicycle{
    // In millimeters (mm)
    private int tireWidth;

    public RoadBike(int startCadence,
                    int startSpeed,
                    int startGear,
                    int newTireWidth){
        super(startCadence,
              startSpeed,
              startGear);
        this.setTireWidth(newTireWidth);
    }

    public int getTireWidth(){
      return this.tireWidth;
    }

    public void setTireWidth(int newTireWidth){
        this.tireWidth = newTireWidth;
    }

    public void printDescription(){
        super.printDescription();
        System.out.println("The RoadBike" + " has " + getTireWidth() +
            " MM tires.");
    }
}

请注意,再次重写了 printDescription 方法。这次,显示有关轮胎宽度的信息。

总而言之,有三个类:BicycleMountainBikeRoadBike。这两个子类覆盖 printDescription 方法并打印唯一信息。

这是一个测试程序,它创建了三个 Bicycle 变量。每个变量都分配给三个自行车类别中的一个。然后打印每个变量。

public class TestBikes {
  public static void main(String[] args){
    Bicycle bike01, bike02, bike03;

    bike01 = new Bicycle(20, 10, 1);
    bike02 = new MountainBike(20, 10, 5, "Dual");
    bike03 = new RoadBike(40, 20, 8, 23);

    bike01.printDescription();
    bike02.printDescription();
    bike03.printDescription();
  }
}

以下是测试程序的输出:

Bike is in gear 1 with a cadence of 20 and travelling at a speed of 10. 

Bike is in gear 5 with a cadence of 20 and travelling at a speed of 10. 
The MountainBike has a Dual suspension.

Bike is in gear 8 with a cadence of 40 and travelling at a speed of 20. 
The RoadBike has 23 MM tires.

Java 虚拟机(JVM)为每个变量中引用的对象调用适当的方法。它不会调用由变量类型定义的方法。此行为称为 virtual method invocation (虚拟方法调用),并演示了 Java 语言中重要多态性功能的一个方面。


Previous page: Overriding and Hiding Methods
Next page: Hiding Fields