问题描述
我正在阅读官方 Oracle 教程,其中以 3 个类的类层次结构示例介绍了多态性的概念;Bicycle 是超类,MountainBike 和 RoadBike 是 2 个子类.
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.
它展示了 2 个子类如何通过声明它的不同版本来覆盖在 Bicycle 中声明的方法printDescription".
It shows how the 2 subclasses override a method "printDescription" declared in Bicycle, by declaring different versions of it.
最后,教程最后提到了 Java 虚拟机 (JVM) 为每个变量中引用的对象调用适当的方法.
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.
但是,关于多态的教程没有提到抽象"类和方法的概念.除非将 Bicycle 中的 printDescription() 声明为抽象",否则如何实现运行时多态性?我的意思是,给出这个例子,编译器根据什么提示决定在编译时不将方法调用绑定到引用类型,并认为它应该让 JVM 在运行时处理?
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?
下面是使用的例子:
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("
Bike 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();
}
}
推荐答案
为什么你认为抽象类会改变任何东西?抽象类主要做两件事
Why would you think abstract classes would change anything? Abstract classes do 2 primary things
- 允许程序员声明一个自身不能被实例化的类,强制子类化,以及
- 允许程序员通过声明方法抽象来强制子类提供方法的实现.
请注意,第 2 点并不意味着除非在基类上将方法声明为抽象方法,否则多态性将不起作用;相反,它为开发人员提供了强制子类提供实现的机会,这在不涉及任何抽象用法的子类化场景中是不需要的.
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.
就是这样.换句话说,抽象的概念补充了 Java 的多态性——它是一种语言特性,但与 Java 在运行时用来调用方法的动态调度没有任何关系.任何时候在实例上调用方法时,都会使用运行时实例的类型来确定要使用哪个方法实现.
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 中的运行时多态性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!