多态性

多态性的字典定义是指生物学中的原理,其中生物体或物种可以具有许多不同的形式或阶段,这个原则也可以应用于面向对象的编程和像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字段,这是一个字符串值,表示自行车是否有前减震器,或者,自行车有一个前后减震器。

这是更新的类:

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)为每个变量中引用的对象调用适当的方法,它不会调用由变量类型定义的方法,此行为称为虚方法调用,并演示Java语言中重要多态特征的一个方面。


上一篇:重写和隐藏方法
下一篇:使用super关键字

博弈
2.5k 声望1.5k 粉丝

态度决定一切