Java中的运行时多态性没有“抽象”? [英] Run-time Polymorphism in Java without "abstract"?

查看:147
本文介绍了Java中的运行时多态性没有“抽象”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读官方的Oracle教程,其中介绍了多态性的概念,以及3个类的类层次结构的示例;自行车是超类,MountainBike和RoadBike是2个子类。



它显示了2个子类如何覆盖在Bicycle中声明的方法printDescription,通过声明它的不同版本。



最后,教程最后提到Java虚拟机(JVM)为每个变量中引用的对象调用适当的方法。 p>

但是,多态性教程没有提到抽象类和方法的概念。除非将Bicycle中的printDescription()声明为abstract,否则如何实现运行时多态性?我的意思是,在这个例子中,基于什么提示,编译器决定在编译时不将方法调用绑定到引用类型,并认为它应该让它在运行时处理JVM?



以下是使用的示例:

 公共类自行车{
public int cadence;
public int gear;
public int speed;

public Bicycle(int startCadence,int startSpeed,int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

public void setCadence(int newValue){
cadence = newValue;
}

public void setGear(int newValue){
gear = newValue;
}

public void applyBrake(int decrement){
speed - = decrement;
}

public void speedUp(int increment){
speed + = increment;
}

public void printDescription(){
System.out.println(\ nBike is+in gear+ this.gear
+以+ this.cadence +
的节奏并以+ this.speed +的速度行进。);
}

}

公共类MountainBike扩展自行车{
私人字符串暂停;

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有一个+
getSuspension()+暂停。);
}

}

公共类RoadBike延伸自行车{

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
有+ getTireWidth()+
MM轮胎。);
}
}


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

bike01 =新自行车(20,10,1);
bike02 =新的MountainBike(20,10,5,Dual);
bike03 =新RoadBike(40,20,8,23);

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


解决方案


除非
Bicycle中的printDescription()被声明为abstract,否则如何实现运行时多态性?




<为什么你认为抽象类会改变什么?抽象类做2个主要的事情


  1. 允许程序员声明一个本身不能实例化的类,强制子类化和

  2. 允许程序员通过声明方法abstract来强制子类提供方法的实现。

注意点2并不意味着除非在基类上将方法声明为抽象,否则多态将不起作用;相反,它为开发人员提供了一个强制子类来提供实现的机会,这在子类化不需要任何抽象用法的场景中是不需要的。



就是这样。换句话说,抽象概念称赞Java的多态性 - 它是一种语言特性,但与Java在运行时用于调用方法的动态调度没有任何关系。无论何时在实例上调用方法,运行时的实例类型都将用于确定要使用的方法实现。


I was going over the official Oracle tutorial where it introduces the idea of polymorphism with the example of a class hierarchy of 3 classes; Bicycle being the superclass, and MountainBike and RoadBike being 2 subclasses.

It shows how the 2 subclasses override a method "printDescription" declared in Bicycle, by declaring different versions of it.

And finally, toward the end, the tutorial mentions the Java Virtual Machine (JVM) calls the appropriate method for the object that is referred to in each variable.

But, nowhere does the tutorial on polymorphism mention the concept of "abstract" classes and methods. How is run-time polymorphism achieved unless printDescription() in Bicycle is declared "abstract"? I mean, given this example, based on what hints does the compiler decide not to bind method invocation to the reference type at compile time, and think that it should leave it for the JVM to deal with at run-time?

Below is the example used:

public class Bicycle {
    public int cadence;
    public int gear;
    public int speed;

    public Bicycle(int startCadence, int startSpeed, int startGear) {
      gear = startGear;
      cadence = startCadence;
      speed = startSpeed;
    }

    public void setCadence(int newValue) {
      cadence = newValue;
    }

    public void setGear(int newValue) {
      gear = newValue;
    }

    public void applyBrake(int decrement) {
      speed -= decrement;
    }

    public void speedUp(int increment) {
      speed += increment;
    }

    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 + ". ");
    }

}

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.");
  }

}

public class RoadBike extends Bicycle{

  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.");
  }
}


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();
      }
}

解决方案

How is run-time polymorphism achieved unless printDescription() in Bicycle is declared "abstract"?

Why would you think abstract classes would change anything? Abstract classes do 2 primary things

  1. Allow the programmer to declare a class that cannot itself be instantiated, forcing subclassing, and
  2. Allow the programmer to force subclasses to provide implementations of methods, by declaring the method abstract.

Note that point 2 does not imply that polymorphism won't work unless a method is declared abstract on the base class; rather, it provides the developer an opportunity to force a subclass to provide an implementation, which is not required in subclassing scenarios that don't involve any abstract usage.

That's it. In other words, the notion of abstract compliments Java's polymorphism -- it is a language feature, but doesn't have anything to do with the dynamic dispatch Java uses at runtime to invoke methods. Anytime a method is invoked on an instance, the type of the instance at runtime is used to determine which method implementation to use.

这篇关于Java中的运行时多态性没有“抽象”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆