我正在阅读一本关于Android编程的书,在开头的章节中有一篇关于Java的小参考指南.但是,我对一些我不太了解的隐式参数进行了讨论.
他定义了Car类:
public class Car {
public void drive() {
System.out.println("Going down the road!");
}
}
Run Code Online (Sandbox Code Playgroud)
然后他继续说:
public class JoyRide {
private Car myCar;
public void park(Car auto) {
myCar = auto;
}
public Car whatsInTheGarage() {
return myCar;
}
public void letsGo() {
park(new Ragtop()); // Ragtop is a subclass of Car, but nevermind this.
whatsInTheGarage().drive(); // This is the core of the question.
}
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道当JoyRide不是Car的扩展时我们如何从类Car调用drive().是因为方法whatsInTheGarage()是返回类型Car,因此它"以某种方式"继承了该类?
谢谢.